How Do I use logical operators

Question:

I have tried everything and if you don’t choose “ST” it constantly loops round the while loop. I am not sure what to do, and it would be super helpful if anyone could tell me. I added the code at the top for some context; I only need help with the while loop. I am using the while loop so if they do not choose a given position, they have to re-choose.

Here is my code:

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")
Asked By: Dan S

||

Answers:

This part is wrong:

while pos != "ST" and "MID" and "DEF" and "GK" and "St" and "Mid" and "Def" and "Gk":

pos != "ST" is evaluated, and the rest of the strings are not compared to anything. In fact, that part is evaluated like:

while (pos != "ST") and ("MID") and ("DEF") and ("GK") and ("St") and ("Mid") and ("Def") and ("Gk"):

Non-empty strings are always True, thus as long as pos != "ST" is True, it’ll never get out of the loop. What you probably meant to do was:

while pos != "ST" and pos != "MID" and pos != "DEF" and pos != "GK" and pos != "St" and pos != "Mid" and pos != "Def" and pos != "Gk":

But, as one of the comments already pointed out, you could just use in:

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

Note that I used a set here since they provide much more efficient membership tests. Might not matter much in this small example, but nevertheless it’s a better choice.

Answered By: Ayxan Haqverdili

!= only applies to the item listed directly before after it (without getting into operations with parens and whatnot). So in your example, your while loop is saying “while position is not equal to ST, and MID is true and DEF is true and DK is true and Mid is true and Def is true and Gk is true, do your statements.

To tell your program to do your while loop while the position is not equal to ST, nor MID, nor DEF, etc, you would have to explicitly spell that out-

while pos != "ST" and pos != "MID" and ... and post != "Gk"
Answered By: Hollywood

The while loop never finishes because your input is outside. So here is the working code:

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)

and you have to choose with “”, because its a string

Answered By: nerd100