Adj*_*con 3 python user-input while-loop
Python新手在这里很抱歉我确定这是一个愚蠢的问题,但我似乎无法在一个教程中解决以下挑战,该教程要求我使用while循环来检查有效的用户输入.
(使用Python2.7)
这是我的代码,但它无法正常工作:
choice = raw_input('Enjoying the course? (y/n)')
student_surveyPromptOn = True
while student_surveyPromptOn:
if choice != raw_input('Enjoying the course? (y/n)'):
print("Sorry, I didn't catch that. Enter again: ")
else:
student_surveyPromptOn = False
Run Code Online (Sandbox Code Playgroud)
以上打印到控制台:
Enjoying the course? (y/n) y
Enjoying the course? (y/n) n
Sorry, I didn't catch that. Enter again:
Enjoying the course? (y/n) x
Sorry, I didn't catch that. Enter again:
Enjoying the course? (y/n)
Run Code Online (Sandbox Code Playgroud)
这显然是不正确的 - 当用户输入'y'或'n'时循环应该结束但我不知道如何做到这一点.我在这做错了什么?
注意:挑战要求我同时使用!=运算符和loop_condition
你可以使用这个条件
while choice not in ('y', 'n'):
choice = raw_input('Enjoying the course? (y/n)')
if not choice:
print("Sorry, I didn't catch that. Enter again: ")
Run Code Online (Sandbox Code Playgroud)