为了账号安全,请及时绑定邮箱和手机立即绑定

在 python 中验证输入

在 python 中验证输入

小唯快跑啊 2022-08-11 17:54:18
我试图写一个石头剪刀枪程序,其中选择是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 inputuser_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 3while 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)
查看完整描述

4 回答

?
陪伴而非守候

TA贡献1757条经验 获得超8个赞

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

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]


查看完整回答
反对 回复 2022-08-11
?
手掌心

TA贡献1942条经验 获得超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

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


查看完整回答
反对 回复 2022-08-11
?
江户川乱折腾

TA贡献1851条经验 获得超5个赞

您的不正确,因为输入返回字符串类型,并且您正在使用类型int进行检查,因此请在循环中更改类型。此外,如果希望它在其中一种情况下终止,则不能使用,在这种情况下,您必须使用 。orand


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)


查看完整回答
反对 回复 2022-08-11
?
收到一只叮咚

TA贡献1821条经验 获得超4个赞

获得输入后,您可以检查两个输入是否都是有效数字并且它是否在有效范围内,如果两个条件之一不成立,请再次请求输入。


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


查看完整回答
反对 回复 2022-08-11
  • 4 回答
  • 0 关注
  • 166 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信