检查 if 语句中的字符串
Check for a string in if-statement
我有一个包含一些用户输入的程序,我需要检查用户输入的是字符串还是 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'
我不知道如何解决这个问题。当我错误地输入一个字符串时,我的程序总是终止。
谢谢!
您可以使用 .isdigit()
检查字符串是否由数字组成,以确保它可以转换为整数:
while True:
in = raw_input("Enter a value between 1 and 10 million: ")
if in.isdigit():
number = int(in)
if number < 1:
print "Must be higher than 1"
continue
elif number > 10**6:
print "Must be less than 10.000.000"
continue
else:
'execute the rest of the code'
else:
print "Must be an integer value!"
continue
首先,您使用的是 Python 2,它将愉快地将字符串与整数进行比较。你不想那样做。其次,raw_input()
将 总是 return 一个字符串。您希望做的是检查该字符串是否可能 代表 一个数字。第三,10.000.000
是不正确的语法。不要使用分隔符。第四,如果你想早到达循环顶部,你只需要continue
。如果循环结束时所有内容都在 if..elif..else
块中,则只会执行其中一个,因此您不需要在每个分支的末尾放置 continue
。您可以使用 continue
语句或重组您的分支。最后,不要使用 in
作为变量名,因为那是一个 Python 关键字。
while True:
inp = raw_input("Enter a value between 1 and 10 million: ")
if not inp.isdigit():
print "Must be an integer value!"
continue # each of these continue statements acts like a "failed, try again"
inp = int(inp)
if inp < 1:
print "Must be higher than 1"
continue # same for this one
if inp > 10000000:
print "Must be less than 10.000.000"
continue # and this one
# execute the rest of the code
我不知道你是怎么想出你的代码的,但这里有不同的建议:
不要使用 "in" 作为变量名,因为它是一个 python 运算符。
输入后可以判断是int还是string
您的程序可能如下所示:
while True:
try:
input_int = int(raw_input("Enter a value between 1 and 10 million: "))
if input_int < 1 :
print "Must be higher than 1"
elif input_int > 10**7:
print "Must be less than 10.000.000"
except:
print "Must be an integer value!"
else: #You can use else with try/except block
#'execute the rest of the code'
瞧
我有一个包含一些用户输入的程序,我需要检查用户输入的是字符串还是 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'
我不知道如何解决这个问题。当我错误地输入一个字符串时,我的程序总是终止。
谢谢!
您可以使用 .isdigit()
检查字符串是否由数字组成,以确保它可以转换为整数:
while True:
in = raw_input("Enter a value between 1 and 10 million: ")
if in.isdigit():
number = int(in)
if number < 1:
print "Must be higher than 1"
continue
elif number > 10**6:
print "Must be less than 10.000.000"
continue
else:
'execute the rest of the code'
else:
print "Must be an integer value!"
continue
首先,您使用的是 Python 2,它将愉快地将字符串与整数进行比较。你不想那样做。其次,raw_input()
将 总是 return 一个字符串。您希望做的是检查该字符串是否可能 代表 一个数字。第三,10.000.000
是不正确的语法。不要使用分隔符。第四,如果你想早到达循环顶部,你只需要continue
。如果循环结束时所有内容都在 if..elif..else
块中,则只会执行其中一个,因此您不需要在每个分支的末尾放置 continue
。您可以使用 continue
语句或重组您的分支。最后,不要使用 in
作为变量名,因为那是一个 Python 关键字。
while True:
inp = raw_input("Enter a value between 1 and 10 million: ")
if not inp.isdigit():
print "Must be an integer value!"
continue # each of these continue statements acts like a "failed, try again"
inp = int(inp)
if inp < 1:
print "Must be higher than 1"
continue # same for this one
if inp > 10000000:
print "Must be less than 10.000.000"
continue # and this one
# execute the rest of the code
我不知道你是怎么想出你的代码的,但这里有不同的建议:
不要使用 "in" 作为变量名,因为它是一个 python 运算符。
输入后可以判断是int还是string
您的程序可能如下所示:
while True:
try:
input_int = int(raw_input("Enter a value between 1 and 10 million: "))
if input_int < 1 :
print "Must be higher than 1"
elif input_int > 10**7:
print "Must be less than 10.000.000"
except:
print "Must be an integer value!"
else: #You can use else with try/except block
#'execute the rest of the code'
瞧