我已经尝试了所有方法,如果您不选择“ ST”,则它会在while循环中不断循环。我不确定该怎么做,如果有人告诉我,这将非常有帮助。在某些情况下,我在顶部添加了代码。我只需要while循环的帮助。我正在使用while
循环,因此,如果他们不选择给定的职位,就必须重新选择。
这是我的代码:
pos = input("What Is Your Choice")
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)
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")
答案 0 :(得分:1)
这部分是错误的:
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"}:
请注意,我在这里使用了set,因为它们提供了效率更高的成员资格测试。在这个小例子中可能没什么大不了,但是它是一个更好的选择。
答案 1 :(得分:0)
!=仅适用于紧接其后列出的项目(不使用括号和其他操作)。因此,在您的示例中,您的while循环说“ while位置不等于ST,MID为true,DEF为true,DK为true,Mid为true,Def为true,Gk为true,”。 >
要告诉您的程序在位置不等于ST,MID或DEF等时执行while循环,您必须明确地将其拼写出来-
while pos != "ST" and pos != "MID" and ... and post != "Gk"
答案 2 :(得分:0)
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)
,您必须选择“”,因为它是一个字符串