【发布时间】:2015-12-02 06:52:20
【问题描述】:
我有一个包含一些用户输入的程序,我需要检查用户输入的是字符串还是介于 1 到 1000 万之间的整数值。
我的代码看起来像这样(简化):
while True:
inp = raw_input("Enter a value between 1 and 10 million: ")
if inp < 1:
print "Must be higher than 1"
continue
elif inp > 10.000.000:
print "Must be less than 10.000.000"
continue
elif 'inp is a string': #here's my problem
print "Must be an integer value!"
continue
else:
'execute the rest of the code'
我不知道如何解决这个问题。当我输入错误的字符串时,我的程序总是终止。
谢谢!
【问题讨论】:
-
不要命名变量
in。它是 Python 中的保留字。 -
这只是我输入的缩写