Py学习  »  Python

用python验证输入

Salah Zahran • 4 年前 • 813 次点击  

我试图写一个摇滚剪纸程序,其中的选择是1,2,和3。我想验证输入,这样除了这三个选项之外的任何输入都会打印一条消息,说明输入是有效的,并要求用户重新输入数据。我有一些工作,但是,即使我输入了12或3,它仍然会打印消息并要求更多的输入。

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)
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/55385
 
813 次点击  
文章 [ 4 ]  |  最新文章 4 年前
abc
Reply   •   1 楼
abc    4 年前

获取输入后,可以检查两个输入是否都有效 digit 它在有效范围内,如果两个条件中有一个不成立,请再次请求输入。

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: ")

或者更简单

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: ")
Azy_Crw4282
Reply   •   2 楼
Azy_Crw4282    4 年前

您的输入不正确,因为输入返回的是string类型,并且您正在使用int类型进行检查,因此请更改循环中的类型。而且,你不能使用 or 如果希望在其中一个情况为真时终止,则必须使用 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)
tdelaney
Reply   •   3 楼
tdelaney    4 年前

在单个循环中执行整个提示/验证。用户只有满足条件才能继续

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
Chris
Reply   •   4 楼
Chris    4 年前

如果以后要与整数进行比较,则需要将输入从字符串转换为整数。如果你想避免一次又一次的重复逻辑,你可以使用一个列表。

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

while user_choice not in [1,2,3]