theanswer= raw_input("Are you ok with that grade?")
while theanswer:
if theanswer == ("yes"):
print ("good!")
break
elif theanswer == ("no"):
print ("oh well")
break
else:
print "enter yes or no"
我需要做什么才能使它有效,我一直在努力
答案 0 :(得分:7)
您需要在else
声明中进行阻止调用。否则你将有一个无限循环,因为theanswer
将永远为真。就像要求输入一样:
theanswer= raw_input("Are you ok with that grade?")
while theanswer:
if theanswer == ("yes"):
print ("good!")
break
elif theanswer == ("no"):
print ("oh well")
break
else:
theanswer= raw_input("Please enter yes or no")
这是Blocking vs Non-Blocking I/O的好借口。它是任何应用程序中的重要基础。
答案 1 :(得分:3)
或者这(这将输入逻辑与你对答案的处理方式分开):
theanswer = raw_input("Are you ok with that grade?")
while theanswer not in ('yes', 'no'):
theanswer = raw_input('Please enter yes or no')
if theanswer == "yes":
print("good!")
elif theanswer == "no":
print("oh well")
答案 2 :(得分:1)
基本上在你的代码中你有一个while循环运行只有在 theanswer == yes或== no 时才会中断。 您也无法在循环中更改 theanswer 的值,因此=>无限循环。
将此添加到您的代码中:
else:
print "enter yes or no"
theanswer= raw_input("Are you ok with that grade?")
答案 3 :(得分:0)
这可以通过递归来完成
def get_user_input(text):
theanswer= raw_input(text)
if theanswer == 'yes':
print('good!')
elif theanswer == ("no"):
print('oh well')
else:
get_user_input('enter yes or no')
get_user_input('Are you ok with that grade?')