【发布时间】:2015-01-18 20:51:01
【问题描述】:
我一直在尝试找出检查变量类型或 while do 语句的方法,但我很困惑,不知道我的代码发生了什么。 我想检查 end 是否是整数,所以我下定了决心。 非常感谢!!
checked=0
while (checked==0):
end=input('Give me an integer number!')
if isinstance(end,(int)):
checked=1
else:
checked=0
print('This is not an integer!')
【问题讨论】:
-
input的输出将始终是一个字符串,无论您在那里输入什么。你可以做if str(int(end)) == end: -
如果你使用的是 Python 3,
input()总是返回一个字符串。 -
end将始终是一个字符串,因为这是输入返回的内容。 -
考虑到我使用的是 python 2.7.6,我是否必须对你所说的进行任何更改? @L3viathan
-
看起来你可能在追求:stackoverflow.com/questions/23294658/…