简体   繁体   中英

Simple Guessing Game in Python that doesn't work

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!')

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM