简体   繁体   English

如何使用 (while) 和 (try) 语句来处理来自三个选择之一的用户输入错误

[英]how to use (while) and (try) statements to handle errors of user inputs from one of three choices only

I'm a beginner in Python and I need to write an interactive code, in which I ask the user which one do you like x, y or z?我是 Python 的初学者,我需要编写一个交互代码,其中我问用户你喜欢哪个 x、y 或 z? and I want to use (while) loop and (try) statement to do so.我想使用 (while) 循环和 (try) 语句来这样做。

I tried the following:我尝试了以下内容:

q1 = input('Would like to see data of Washington, Chicago or New York? \n')

while q1 == 'Washington'or =='Chicago' or == 'New York'
    try:
        print()
        break
    except:
        print('invalid input, please select a name of a city!')

Have you tried this?你试过这个吗?

q1 = input('Would like to see data of Washington, Chicago or New York? \n')

while q1 == 'Washington' or q1 =='Chicago' or q1 == 'New York':
    try:
        print()
        break
    except:
        print('invalid input, please select a name of a city!')

You need to repeat q1 for each conditional statement...您需要为每个条件语句重复q1 ...

try something like this:尝试这样的事情:

while true:
    q1 = input('Would like to see data of Washington, Chicago or New York? \n')
    if q1 not in [ 'Washington', 'Chicago', 'New York' ]:
        print('invalid input, please select a name of a city!')
    else:
        break

To limit the user's input in some choices it is better to put the choices together in a list then compare the input to them as follows:为了限制用户在某些选择中的输入,最好将这些选择放在一个列表中,然后将输入与它们进行比较,如下所示:

choices = ['Washington', 'Chicago', 'New York']
q1 = input("Would like to see data of Washington, Chicago or New York? \n")
while q1 not in choices:
    print('invalid input, please select a name of a city!')
    q1 = input()

so later on if you want to add more choices you can do it easily by modifying the choices variable.所以以后如果你想添加更多的选择,你可以通过修改 choices 变量轻松地做到这一点。 This code will block in the while loop until the user input is one of the choices.此代码将阻塞在 while 循环中,直到用户输入是其中一个选项。 However, the user input must be exactly (case sensitive) as on of the choices (ie chicago won't work, it should be Chicago with capital 'c').但是,用户输入必须与选项中的一个完全相同(区分大小写)(即芝加哥不起作用,它应该是带有大写“c”的芝加哥)。

My advise (if you don't mind for the exact case sensitive name) is to make the choices all small letter like this:我的建议(如果您不介意区分大小写的确切名称)是像这样选择所有小写字母:

choices = ['washington', 'chicago', 'new york']

then compare the user input (lowercased) to the choices as follows:然后将用户输入(小写)与选项进行比较,如下所示:

while q1.lower() not in choices:
    ...

Solution -1解决方案-1

If you want to achieve continues loop until user enters incorrect value, try below code.如果你想实现继续循环直到用户输入不正确的值,请尝试下面的代码。

while flag:
    q1 = input('Would like to see data of Washington, Chicago or New York? \n')
    try:
        if q1 in [ 'Washington', 'Chicago', 'New York' ]:
            print('your result is: ' + q1)
        else:
            flag=0
            print('Invalid input, please select a name of a city!')
            break
    except:
        flag=0
        print('Invalid input, please select a name of a city!')
        break

Solution - 2解决方案 - 2

We can achieve this by using if-else, however if you want to use while loop then try below code.我们可以通过使用 if-else 来实现这一点,但是如果您想使用 while 循环,请尝试下面的代码。

Editted code编辑代码

q1 = input('Would like to see data of Washington, Chicago or New York? \n')
flag = 0;
while (q1 == 'Washington' or q1 =='Chicago' or q1 == 'New York'):
    try:
        flag = 1
        break
    except:
        print('invalid input, please select a name of a city!')
        
if(flag):
    print('your result is: ' + q1)
else:
    print('Invalid input, please select a name of a city!')

Please try this code:请试试这段代码:

your code is correct but just need to add q1 for all cases in while condition您的代码是正确的,但只需要在 while 条件下为所有情况添加 q1

q1 = input('Would like to see data of Washington, Chicago or New York? \n')

while (q1 == 'Washington' or q1 =='Chicago' or q1 == 'New York'):
    try:
        print('your result is: ' + q1)
        break
    except:
        print('invalid input, please select a name of a city!')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 if 语句中使用多个输入 - how to use multiple inputs with if statements 如何在 while true 循环中处理来自用户输入的 2 个错误 - How can I handle 2 errors from user input in a while true loop 如何使用 while 循环从用户输入中附加具有任意数量的键和多个值的字典 - How can I use a while loop to append a dictionary with an arbitrary number of keys and multiple values from user inputs 如何有效地使用 input().split() 在一行中从用户那里获取至少 2 个输入? - How can i use input().split() effectively to take at least 2 inputs from user in one line? 如何使项目打印三遍,然后在保持用户输入名称的同时开始新的一行 - How to make an item print three times then start a new row while keeping the name of user inputs 如何有效地构造argparse参数中一个,两个或三个选择的选择? - How to structure the selection of one, two, or three choices in an argparse argument efficiently? 使用带有用户输入的 if 语句 - Using if statements with user inputs 如何减少到一个尝试,语句除外 - How to reduce to just one try,except statements while 循环中的三个 If 语句 - Three If statements in while loops 如何仅使用当前用户的authors(fk)来修改通用CreateView中的作者选择字段? - How to modify the author choices field in my generic CreateView with authors(fk) only from the current user?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM