【问题标题】:How Do I use logical operators [duplicate]如何使用逻辑运算符 [重复]
【发布时间】:2018-11-13 17:40:30
【问题描述】:

我已经尝试了所有方法,如果您不选择“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")

【问题讨论】:

  • and 运算符比较逻辑语句而不是值本身。试试while pos != "ST" and pos != "MID" ...:
  • while pos not in {"ST" , "MID", "DEF" ,"GK" ,"St","Mid","Def" , "Gk"}:
  • 也可以使用in操作符:while pos not in ["ST", "MID", ...]

标签: python python-3.x logical-operators


【解决方案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":

但是,正如其中一位 cmets 已经指出的那样,您可以只使用 in

while pos not in {"ST", "MID", "DEF", "GK", "St", "Mid", "Def", "Gk"}:

请注意,我在这里使用了set,因为它们提供了更有效的成员资格测试。在这个小例子中可能无关紧要,但它仍然是一个更好的选择。

【讨论】:

    【解决方案2】:

    != 仅适用于紧随其后列出的项目(不涉及带括号和诸如此类的操作)。因此,在您的示例中,您的 while 循环说“当位置不等于 ST,MID 为真且 DEF 为真且 DK 为真且 Mid 为真且 Def 为真且 Gk 为真时,请执行您的陈述。

    要告诉你的程序在位置不等于 ST、MID、DEF 等时执行 while 循环,你必须明确地拼写出来-

    while pos != "ST" and pos != "MID" and ... and post != "Gk"
    

    【讨论】:

    • 谢谢你,希望我下次会记住这一点
    【解决方案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 声明等于while pos != "ST"
    猜你喜欢
    • 2019-04-21
    • 2011-03-14
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-22
    • 1970-01-01
    相关资源
    最近更新 更多