簡體   English   中英

驗證python中的輸入

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM