简体   繁体   中英

Validating input in python

Im trying to write a rock paper scissor program where the choice are 1, 2, and 3. I want to validate the input so that any input other than these three choice will print a message saying the input is valid, and ask the user to reinput the data. I have it somewhat working, however, even if i do enter 1 2 or 3, it will still print the message and ask for more input.

print("This program simulates a 'rock paper scissor' game.")
print("The rules are as follows: rock beats scissor, paper beats rock, and scissor beats paper. \n")

print("This program simulates a 'rock paper scissor' game.")
print("The rules are as follows: rock beats scissor, paper beats rock, and scissor beats paper. \n")

#get user input
user_choice = input("Please choose from the following:  \n"
                    " 1 for scissor \n"
                    " 2 for rock \n"
                    " 3 for paper. \n")

#validate input so user only enters 1, 2, or 3
while user_choice != 1 or user_choice != 2 or user_choice != 3:
    user_choice = input("Please enter only '1' for scissor, '2' for rock, or '3' for paper: ")

#convert input to int 
int_user_choice = int(user_choice)

You will need to convert the input from a string to an integer if you want to compare it with integers later. And you can use a list if you want to avoid repeating the logic over and over again.

user_choice = int(input("Please choose from the following:  \n"
                    " 1 for scissor \n"
                    " 2 for rock \n"
                    " 3 for paper. \n"))

and

while user_choice not in [1,2,3]

After you get the input, you could check if both the input is a valid digit and it is in the valid range and if one of the two conditions do not hold, ask again for an input.

valid_choices = {1,2,3}
while not user_choice.isdigit() and not int(user_choice) in valid_choices:
   user_choice = input("Please enter only '1' for scissor, '2' for rock, or '3' for paper: ")

or more simply

valid_choices = {'1','2','3'}
while not user_choice in valid_choices:
  user_choice = input("Please enter only '1' for scissor, '2' for rock, or '3' for paper: ")

Do the entire prompt / validation in a single loop. User doesn't get to continue until the condition is satisfied

print("This program simulates a 'rock paper scissor' game.")
print("The rules are as follows: rock beats scissor, paper beats rock, and scissor beats paper. \n")

print("This program simulates a 'rock paper scissor' game.")
print("The rules are as follows: rock beats scissor, paper beats rock, and scissor beats paper. \n")

#get user input
while True:
    user_choice = input("Please choose from the following:  \n"
                    " 1 for scissor \n"
                    " 2 for rock \n"
                    " 3 for paper. \n")

    #validate input so user only enters 1, 2, or 3
    if user_choice in ["1", "2", "3"]:
        int_user_choice = int(user_choice)
        break

Yours is not correct because input returns of type string and you were checking with type int, so change the type in the loop. Moreover, you cannot use or if you want it to terminate when one of the cases is true, in that case, you must use and .

print("This program simulates a 'rock paper scissor' game.")
print("The rules are as follows: rock beats scissor, paper beats rock, and scissor beats paper. \n")

print("This program simulates a 'rock paper scissor' game.")
print("The rules are as follows: rock beats scissor, paper beats rock, and scissor beats paper. \n")

#get user input
user_choice = input("Please choose from the following:  \n"
                    " 1 for scissor \n"
                    " 2 for rock \n"
                    " 3 for paper. \n")

#validate input so user only enters 1, 2, or 3

while user_choice != '1' and user_choice != '2' and user_choice != '3':
    user_choice = input("Please enter only '1' for scissor, '2' for rock, or '3' for paper: ")

#convert input to int
int_user_choice = int(user_choice)

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