亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 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條經驗 獲得超5個贊

獲得輸入后,您可以檢查兩個輸入是否都是有效數字并且它是否在有效范圍內,如果兩個條件之一不成立,請再次請求輸入。


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 關注
  • 209 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號