简体   繁体   中英

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? and I want to use (while) loop and (try) statement to do so.

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...

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. This code will block in the while loop until the user input is one of the choices. 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').

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

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

We can achieve this by using if-else, however if you want to use while loop then try below code.

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

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!')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM