Reputation: 5
I am trying to make a simple guessing game. When I run the code and print the number in advance to check if the program is working, it keeps on having the same wrong response. In other words, even if the guess variable is equal to the num
variable, the program still returns "Incorrect!", and I can't figure out why. Thank you in advance. The code is pretty self-explanatory, so I'll post it here.
import random
num = random.randint(1, 6)
print(num)
guess = input(f'Guess a number: ')
if guess == num:
print(f'Correct!')
else:
print(f'Incorrect!')
Upvotes: 0
Views: 99
Reputation: 8980
You are comparing different types. num
is an integer number while the guess
is a string. You need to convert it to an integer before comparing them.
Try using num == int(guess)
instead.
Upvotes: 3