While 循环检查有效的用户输入? [英] While loop to check for valid user input?

查看:31
本文介绍了While 循环检查有效的用户输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的 Python 新手很抱歉我确定这是一个愚蠢的问题,但我似乎无法解决教程中的以下挑战,该教程要求我使用 while 循环来检查有效的用户输入.

Python newbie here so sorry for what I'm sure is a stupid question, but I can't seem to solve the following challenge in a tutorial that is asking me to use a while loop to check for valid user input.

(使用 Python2.7)

(using Python2.7)

这是我的代码,但它不能正常工作:

Here's my code, but it's not working properly:

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 

上面打印到控制台:

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)  

这显然是不正确的 - 当用户输入y"或n"时,循环应该结束,但我不确定如何执行此操作.我在这里做错了什么?

Which obviously isn't correct — the loop should end when the user enters either 'y' or 'n' but I'm not sure how to do this. What am I doing wrong here?

注意:挑战要求我同时使用 != 运算符和 loop_condition

Note: the challenge requires me to use both the != operator and the loop_condition

推荐答案

更短的解决方案

while raw_input("Enjoying the course? (y/n) ") not in ('y', 'n'):
    print("Sorry, I didn't catch that. Enter again:")

你的代码做错了什么

关于你的代码,你可以添加一些打印如下:

What your code is doing wrong

With regard to your code, you can add some print as follow:

choice = raw_input("Enjoying the course? (y/n) ")
print("choice = " + choice)
student_surveyPromptOn = True
while student_surveyPromptOn:
    input = raw_input("Enjoying the course? (y/n) ")
    print("input = " + input)
    if choice != input:
        print("Sorry, I didn't catch that. Enter again:")
    else:
        student_surveyPromptOn = False

以上打印出来:

Enjoying the course? (y/n) y
choice = y
Enjoying the course? (y/n) n
choice = y
input = n
Sorry, I didn't catch that. Enter again:
Enjoying the course? (y/n) x
choice = y
input = x
Sorry, I didn't catch that. Enter again:
Enjoying the course? (y/n) 

如您所见,您的代码中有第一步出现问题,您的答案会初始化 choice 的值.这就是你做错了.

As you can see, there is a first step in your code where the question appears and your answer initializes the value of choice. This is what you are doing wrong.

如果您必须同时使用 != 运算符和 loop_condition,那么您应该编码:

If you have to use both the != operator and the loop_condition then you should code:

student_surveyPromptOn = True
while student_surveyPromptOn:
    choice = raw_input("Enjoying the course? (y/n) ")
    if choice != 'y' and choice != 'n':
        print("Sorry, I didn't catch that. Enter again:")
    else:
        student_surveyPromptOn = False

但是,在我看来,Cyber​​ 的解决方案和我更短的解决方案都更优雅(即更 Python 化).

However, it seems to me that both Cyber's solution and my shorter solution are more elegant (i.e. more pythonic).

这篇关于While 循环检查有效的用户输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆