我的代码存在问题,我也无法找到解决方案。我问的问题必须是有效的,但循环只是继续,让我输入。
print('Do you want to go to the store or woods?')
lists = ('woods', 'store')
while True:
answers = input()
if answers == 'store':
break
print('Going to the store...')
elif answers == 'woods':
break
print('Going to the woods...')
while lists not in answers:
print('That is not a valid answer')
答案 0 :(得分:2)
您想要检查用户的答案是否不在您的有效答案列表中。你正在做的事情是相反的。试试这个:
if answers not in lists:
print('That is not a valid answer')
此时您还想要break
,或者再次打印提示信息。
答案 1 :(得分:1)
试试这个:
print('Do you want to go to the store or woods?')
places = ('woods', 'store')
while True:
answer = input()
if answer in places:
print ("Going to the {0}...".format(answer))
break
else:
print('That is not a valid answer')
答案 2 :(得分:1)
首先,您的print
语句无法访问。您可以找到更多信息here。
#...
if answers == 'store':
print('Going to the store...')
break
elif answers == 'woods':
print('Going to the woods...')
break
#...
然后,你的第二个while
语句就没有意义了。如果您只想打印That is not a valid answer
,以防输入与store
或woods
不同,并为用户再试一次 - 那么您可以使用else
,而不是{{1}总之:
lists
如果您想检查,是否在print('Do you want to go to the store or woods?')
# no lists
while True:
answers = input()
if answers == 'store':
print('Going to the store...')
break
elif answers == 'woods':
print('Going to the woods...')
break
else:
print('That is not a valid answer')
中遇到了用户的输入,那么您需要在内部执行此lists
操作:
in