Python if statement not working

In my code, it is skipping my if statement and just goes to the else.

1 Like

Please post the code you have written so far, and include a link to the exercise page so we can follow up.

1 Like


print ("Hello, player. This is a number game. You pick a number and I pick a number. If our numbers are the same, you win!")
print ("But, there is a catch. Each time you guess wrong, the game gets harder, so, you better guess right the first time!!")
no = input ("pick a number from 1 - 3 inclusive") 
import random
no2 = random.randint(2)
print (no2)
if no == no2:
 print("Congrats, you won")
    print ("Sorry, try again, you have 2 more lives")
    no11 = input ("pick a number from 1 - 4 inclusive") 
    import random
    no3 = random.randint(1, 4)
    print (1)
    if no11 == 1:
        print("Congrats, you won")
        print("Sorry, try again, but this is your last life")
        no12 = input ("pick a number from 1 - 5 inclusive") 
        import random
        no4 = random.randint(1, 5)
        print (no4)
        if no12 == no4:
            print("Congrats, you won")
            print("You lose boohoo")

I actually made this code myself, so I have no link.

There should really be only one import statement at the very top of the program. We can then call upon its modules as often as we need to. They will stay in memory (be persistent) for the remainder of the session.

It looks as though some of the indentation is inconsistent and may be nesting code that isn’t meant to be nested. If on the other hand, your intention is to have them nested, then it may be a good idea to rethink the logic so it is linear, top down in execution with no nesting.

1 Like

import something
should be at the top (always). That way it will be easy to read and understand. Also once you import something, you need not to import it again.

5th line: randint needs 2 parameters (see below)
random.randint(a, b)

so change to this: no2 = random.randint(1, 4)

7th line:
if no == no2:
Here you are comparing string with int
you have have written this no = input('something') which is string by default.
any input you get through input() will be considered as a string by default.
so you have to specify the datatype INT

change to this: no = int(input('pick a number from 1 - 3 inclusive))
this will take input as integer

1 Like

Thank you. My code works well now.

1 Like