[英]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.