首先,您的print
陈述是不可访问的。您可以在此处找到更多信息。
#...
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
不需要: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
内到外执行此技巧:
print('Do you want to go to the store or woods?')
lists = ('woods', 'store')
while True:
answers = input()
if answers == 'store':
print('Going to the store...')
break
elif answers == 'woods':
print('Going to the woods...')
break
elif answers not in lists:
print('That is not a valid answer')
else:
# some default case, for example sys.exit() (needs sys to be imported)