我正在做一個專案,我需要撰寫一個函式來詢問用戶想要使用哪種型別的 Minecraft 服務器軟體。然后我需要更改它是否是選項之一,如果不是,讓用戶重新輸入他們的選擇。這是目前我用來這樣做的代碼:
config_type_check = True
while config_type_check == True:
if configuration_type == "vanilla" or "forge" or "spigot" or "bukkit" or "paper":
config_type_check = False
else:
configuration_type = input("You have entered an invalid selection. Please enter a valid server installation type.")
我將 config_type_check 用于回圈變數,并將其設定為 false,因此如果用戶輸入有效,代碼可以正常繼續。但這不起作用,它允許任何輸入通過。任何想法為什么?(對不起,如果我寫得亂七八糟,這是我在 StackOverflow 上的第一篇文章!
uj5u.com熱心網友回復:
您的問題是錯誤使用“或”運算子。
config_type_check = True
configuration_type = input(" server installation type: ")
while config_type_check == True:
if configuration_type == "vanilla" or configuration_type =="forge" or configuration_type =="spigot" or configuration_type =="bukkit" or configuration_type =="paper":
config_type_check = False
else:
configuration_type = input("You have entered an invalid selection. Please enter a valid server installation type.")
您也可以使用“in”運算子:
config_type_check = True
configuration_type = input(" server installation type: ")
while config_type_check == True:
if configuration_type in ("vanilla","forge","spigot","bukkit","paper"):
config_type_check = False
else:
configuration_type = input("You have entered an invalid selection. Please enter a valid server installation type.")
玩得開心 :)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/511296.html
標籤:python-3.x变量
