简体   繁体   中英

user input validation for python

I have a very simple question that I can't seem to find the answer to and am very new to python. I need to check a users input to see if the user has entered one of four operations. The operation are '^', '&', '|', and 'q' for quit. The user will be prompt again if they dont enter a valid operation so a while loop is needed, is there a way to test for all the operations in a single line? here is the code I have so far

    operation = input("enter an operation to be used\n")
    print('operation chosen:', operation)
    error = ("please enter |, &, ^, or q" )
    while operation != '&' 
    print(error)

This setup will only accept inputs that are in (^&|q) the loop will cycle until user inputs of of those choices, the beginning initializes operation to a value not in the list to start the loop.

operation = 'x'
while operation not in ('^&|q'):
    operation = input('Enter operation from "^%|q": ')
print('Operation chosen: {}'.format(operation))
 Enter operation from "^%|q": a Enter operation from "^%|q": s Enter operation from "^%|q": d Enter operation from "^%|q": ^ Operation chosen: ^ 

This route will do the same and prompt the user with a Invalid message, although this route requires a input before the loop to initialize operation

operation = input('Enter operation from "^&|q": ')
while operation not in ('^&|q'):
    operation = input('Invalid entry. Please choose from "^%|q": ')
print('Operation chosen: {}'.format(operation))

You can store all valid operations in an array and check the input against that array; correct input should break the loop, an incorrect input should raise the error and loop again.

Sample code below

allowd_operations= ['|','&','^', 'q' ]
operation = ''

while True:
    operation = input("enter an operation to be used\n")
    if operation in allowd_operations:
        # correct input
        break

    print('operation chosen:', operation)
    error = ("please enter |, &, ^, or q" )
    print(error)

Welcome to your Python journey. Here's a way to test user input with recursion and a function, which is helpful for larger projects, and those where some of these variables can change:

def checkinput(userinput):
    if userinput in ('|^&q'):
        return userinput
    else:
        loopinputline = input("please enter |, &, ^, or q\n" )
        return checkinput(loopinputline)


operation = str(input("enter an operation to be used\n"))
finalinput = checkinput(operation)
print('operation chosen:', finalinput)
list1=['^','&','|','q']
operation = raw_input('Enter operation from "^&|q": ')
while operation not in list1:
    operation = raw_input('Invalid entry. Please choose from "^&|q": ')
print(operation)

as per the question asked the user has to enter only one option. for answer by @cash_the stampede , if the user enters '^&' still it is accepting as the right answer, which should not be the case. so the above is just small correction.

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