-2

I am trying build this thing where: if you type wrong answer the program will run again and if you write correct one it should stop but even after writing correct statement it is not stopping

EvenOdd = 1

def choosegame():
    while True:
        global gname
        gname = input("\nWhich game would you like to play?\n    1.Even&Odd\n         OR         \n    2.Three lines\n\tType the number of the game to start\n>>>")
        if "1" or "Even&Odd" or "even&odd" or "evenodd" or "even" or "odd" in gname:
            gname = EvenOdd
            print("odd even game passed")
            break
        elif "2" or "Three" or "three" or "Lines" or "lines" in gname:
            print("threeline game passes")
            break
        else:
            print("wrong word")
    return choosegame
choosegame()

3 Answers3

0

you are not checking the condition of the variable

try this

EvenOdd = 1

def choosegame():
    while True:

        gname = input("\nWhich game would you like to play?\n    1.Even&Odd\n         OR         \n    2.Three lines\n\tType the number of the game to start\n>>>")
        if gname in ["1", "Even&Odd", "even&odd", "evenodd", "even", "odd"]:
            gname = EvenOdd
            print("odd even game passed")
            break
        elif gname in ["2", "Three", "three", "Lines", "lines"]:
            print("threeline game passed")
            break
        else:
            print("wrong word")
    return gname

choosegame()
devMe
  • 126
  • 10
0

if "1" or "Even&Odd" or "even&odd" or "evenodd" or "even" or "odd" in gname:

This particular line does not behave like you intend it to. This will always evaluate to true because if "1" does not compare values but rather just checks if the value exists or not.

You can better understand this by the snippet below:

my_variable = None

if my_variable:
    print("Yes")
else:
    print("No")  

This will print "No" when my_variable is None but will print "Yes" if my_variable has any type of value.

So you should define explicit checks i.e. if gname == "1" or gname == "even" etc.

chomprrr
  • 406
  • 4
  • 15
0

Your code is inaccurate where it says

if "1" or "Even&Odd" or "even&odd" or "evenodd" or "even" or "odd" in gname:

because the conditions are ("1"), ("Even&Odd"), ... and ("odd" in gname). The conditions which are just strings evaulate to True because any non-empty string is True.

The correct line would be:

if gname in ["1", "Even&Odd", "even&odd", "evenodd", "even", "odd"]:

because this checks if the entered gname is in the list of valid values.