Home > Net >  Why is the elif statement taking priority over my if statement?
Why is the elif statement taking priority over my if statement?

Time:04-08

So I've got a number guessing game, all my previous code works fine, and you can probably see that I have the randomly generated number printed on the screen. But when I guess the number correctly I still get told I am wrong?

num = randrange(0, 100)

if num > 0 and num < 50:

print("The number is greater than 0 but smaller than 50")

elif num > 50 and num < 100:

print("The number is smaller than 100 but greater than 50")

print(num)

print('Start Guessing')

a = input()


a = input()
if a == num:

print ("You got the correct number, woohoo")

elif num > 0 and num < 20:

print ("Hint: The number is bigger than 0 but smaller than 20")

elif num > 20 and num < 40:

print ('Hint: The number is bigger than 20 but smaller than 40')

elif num > 40 and num < 60:

print ('Hint: The number is bigger than 40 but smaller than 60')

elif num > 60 and num < 80:

print ('Hint: The number is bigger than 60 but smaller than 80')

elif num > 80 and num < 100:

print ('Hint: The number is bigger than 80 but smaller than 100')

Sorry for the weird looking code, I've never used this site before.

CodePudding user response:

Your code appears to be Python. You should indicate that in a tag. Also, since Python if statements rely on indentation you should post your code with proper indentation.

In the code:

a = input()
if a == num:

You are comparing a string to a number. You should cast a to an int.

if int(a) == num:
  • Related