我有一个评估输入的函数,我需要一直询问他们的输入,并对其进行评估,直到他们输入一个空行。我该怎么安排呢?
while input != '':
evaluate input我想用这样的东西,但效果不太好。有什么帮助吗?
发布于 2013-12-02 20:47:35
有两种方法可以做到这一点。首先是这样的:
while True: # Loop continuously
inp = raw_input() # Get the input
if inp == "": # If it is a blank line...
break # ...break the loop第二个是这样的:
inp = raw_input() # Get the input
while inp != "": # Loop until it is a blank line
inp = raw_input() # Get the input again注意,如果您使用的是Python3.x,则需要用raw_input替换input。
发布于 2019-01-12 10:42:59
这是一个小程序,它会一直询问输入,直到提供所需的输入为止。
我们应该将所需的数字保留为字符串,否则可能无法工作。默认情况下,输入被视为字符串。
required_number = '18'
while True:
number = input("Enter the number\n")
if number == required_number:
print ("GOT IT")
break
else:
print ("Wrong number try again")或您可以使用eval( use ())方法
required_number = 18
while True:
number = eval(input("Enter the number\n"))
if number == required_number:
print ("GOT IT")
break
else:
print ("Wrong number try again")发布于 2013-12-02 20:49:20
您可能希望使用一个单独的值来跟踪输入是否有效:
good_input = None
while not good_input:
user_input = raw_input("enter the right letter : ")
if user_input in list_of_good_values:
good_input = user_inputhttps://stackoverflow.com/questions/20337489
复制相似问题