询问有效答案时无法完成循环
Can't finish the loop when asking for valid answers
我的代码有问题,我也找不到解决方案。我问的问题必须是有效的,但循环只是继续,让我输入。
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')
您想检查用户的答案是否不在您的有效答案列表中。你正在做的是相反的方式。试试这个:
if answers not in lists:
print('That is not a valid answer')
此时您还需要 break
,或者再次打印您的提示信息。
试试这个:
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')
首先,您的 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
,而不使用 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)
我的代码有问题,我也找不到解决方案。我问的问题必须是有效的,但循环只是继续,让我输入。
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')
您想检查用户的答案是否不在您的有效答案列表中。你正在做的是相反的方式。试试这个:
if answers not in lists:
print('That is not a valid answer')
此时您还需要 break
,或者再次打印您的提示信息。
试试这个:
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')
首先,您的 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
,而不使用 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)