如何使用逻辑运算符

2024-09-27 21:27:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我什么都试过了,如果你不选择“ST”,它就会不断地循环。我不知道该怎么办,如果有人能告诉我,那会非常有帮助。我在顶部添加了一些上下文代码;我只需要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")

Tags: andposiftimeissleepwhatst
3条回答

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)

你必须用“”来选择,因为它是一个字符串

这部分是错误的:

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,因为它们提供了更有效的成员资格测试。在这个小例子中可能不太重要,但是它是一个更好的选择。你知道吗

哦!=仅适用于紧跟在它前面的项目(不使用parens和诸如此类的操作)。所以在你的例子中,while循环说“while position不等于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"

相关问题 更多 >

    热门问题