0

I'm fairly new to Python and I'm trying to create a game where you simulate a 6 sided die roll. The user has to guess a number and the program should tell them if their guess was right or wrong. The problem I have run into is that my program always says the user guessed wrong even when they guessed correctly. I'm just not sure what the problem is. The issue persists no matter what sort of loop I use.

def main():
#importing random
import random
#creating random numbers 1-6 inclusive
roll = random.randint(1, 6)
print(roll)
#taking user input
user_guess = input("Guess a number between 1 and 6: ")
if user_guess == roll:
    print("You guessed correctly!")
    else:
    print("Better luck next time.")

main()

Sam
  • 1
  • `random.randint` returns an integer, `input` returns a string. Strings and integers can never be equal. – Paul M. Apr 12 '21 at 21:32
  • Thank you! I cannot believe I forgot to convert my input to int. Such a simple fix! – Sam Apr 12 '21 at 21:35
  • Does this answer your question? [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) – mkrieger1 Apr 12 '21 at 21:56

1 Answers1

0

here the problem is the type of the variables. You are trying to compare a string(user_guess) with an int(roll).

You can convert the int to the string for instance by doing this:

if user_guess == str(roll):

Then it should work. Good luck.

Jaime38130
  • 167
  • 1
  • 1
  • 14
  • 1
    Yes I was able to get it working after converting my input to an integer. Thanks!! – Sam Apr 13 '21 at 22:13