I'm experimenting a bit and trying to make a simple dice game. I want it to be so one could guess a number between 1-6, then have a dice randomly generate a number, and giving a message depending on if one was right or not.
I have tried:
guess_1 = input("Guess the number on the dice: ")
print("You have entered " + guess_1)
from random import randint
dice = randint(1,6)
if guess_1 == dice:
print(f"The result was {dice}. Congratulations, you win!")
else:
print(f"The result was {dice}. Sorry, try again!")
However, now, even if I guess correctly, I still wind up with "You have entered 6 The result was 6. Sorry, try again!"
Does anyone know what I have done wrong here? And if there is a way to do so that you get an error message if you write e.g. 15 instead of a number within the range of the dice?
All help greatly appreciated!
You are comparing between a string and an integer(when you use "input()", the default data type is string.) You need to convert your input to an integer first, and then compare.
from random import randint
guess_1 = int(input("Guess the number on the dice: "))
print("You have entered " + guess_1)
dice = randint(1,6)
if guess_1 == dice:
print(f"The result was {dice}. Congratulations, you win!")
else:
print(f"The result was {dice}. Sorry, try again!")