3 回答

TA貢獻1825條經驗 獲得超4個贊
這部分是錯誤的:
while pos != "ST" and "MID" and "DEF" and "GK" and "St" and "Mid" and "Def" and "Gk":
pos != "ST"
被評估,其余的字符串不與任何東西進行比較。實際上,該部分的評估方式如下:
while (pos != "ST") and ("MID") and ("DEF") and ("GK") and ("St") and ("Mid") and ("Def") and ("Gk"):
非空字符串總是True
,因此只要pos != "ST"
是True
,它就永遠不會退出循環。你可能想做的是:
while pos != "ST" and pos != "MID" and pos != "DEF" and pos != "GK" and pos != "St" and pos != "Mid" and pos != "Def" and pos != "Gk":
但是,正如已經指出的評論之一,您可以使用in
:
while pos not in {"ST", "MID", "DEF", "GK", "St", "Mid", "Def", "Gk"}:
請注意,我在這里使用了一個集合,因為它們提供了更有效的成員資格測試。在這個小例子中可能無關緊要,但它仍然是一個更好的選擇。

TA貢獻1796條經驗 獲得超4個贊
while 循環永遠不會完成,因為您的輸入在外部。所以這是工作代碼:
import time
pos = ""
while pos != "ST" and "MID" and "DEF" and "GK" and "St" and "Mid" and "Def" and "Gk":
print("What Position Do You Want To Play?")
time.sleep(1)
print("The Options Are..")
time.sleep(1)
print("ST (Striker)")
time.sleep(1)
print("MID (Midfielder)")
time.sleep(1)
print("DEF (Defender)")
time.sleep(1)
print("GK (Goalkeeper)")
time.sleep(1)
pos = input("What Is Your Choice")
break
if pos == "ST":
shot = 8
print("Shot Is",shot)
passing = 6
print("Passing Is",passing)
pace = 6
print("Pace Is",pace)
defending = 2
print("Defending Is",defending)
if pos == "MID":
shot = 6
print("Shot Is",shot)
passing = 6
print("Passing Is",passing)
pace = 6
print("Pace Is",pace)
defending = 4
print("Defending Is",defending)
if pos == "DEF":
shot = 2
print("Shot Is",shot)
passing = 6
print("Passing Is",passing)
pace = 4
print("Pace Is",pace)
defending = 8
print("Defending Is",defending)
if pos == "GK":
dive = 7
dist = 8
catch = 7
print(pos)
你必須選擇“”,因為它是一個字符串
添加回答
舉報