[英]Validating input in python
我正在尝试编写一个石头剪刀布程序,其中的选择是 1、2 和 3。我想验证输入,以便除了这三个选择之外的任何输入都将打印一条消息,说明输入有效,并要求用户重新输入数据。 我有一些工作,但是,即使我输入 1 2 或 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)
如果您想稍后将其与整数进行比较,则需要将输入从字符串转换为整数。 如果您想避免一遍又一遍地重复逻辑,您可以使用列表。
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]
获得输入后,您可以检查输入是否都是有效数字且是否在有效范围内,如果两个条件之一不成立,则再次请求输入。
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: ")
在一个循环中完成整个提示/验证。 在满足条件之前,用户不能继续
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
你的不正确,因为输入返回的是字符串类型,而你正在检查类型是 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.