我正在尝试获取用户输入的数据类型,但是我在代码方面遇到了一些问题。我尝试了以下代码:
def user_input_type():
try:
user_input = int(raw_input("Enter set of characters or digit"))
except:
try:
user_input = str(user_input)
except Exception as e:
print e
return type(user_input)
return type(user_input)
print user_input_type()
但是在运行代码之前它给了我两个警告
user_input
可能在赋值之前被引用李>Exception
李>运行代码后,当我输入数字时,它会给我正确的值,但当我输入字符时,它会给我一个错误:
UnboundLocalError: local variable 'user_input' referenced before assignment
请帮忙
您需要在
try-catch
之外设置“用户输入”例如:
相关问题 更多 >
编程相关推荐