Py学习  »  Python

用python验证输入

Salah Zahran • 4 年前 • 836 次点击  

我试图写一个摇滚剪纸程序,其中的选择是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
 
836 次点击  
文章 [ 4 ]  |  最新文章 4 年前