Reputation: 93
I am new to python and trying to write a program that requires the user to guess a number, 1 - 6 and then they are told if they guessed right or not. However, even when the user guesses right the else statement is still returned.
I apologise for the beginner question, although this really has me stuck because the 'guess' variable is being assigned correctly, I tested this by moving the print("Your guess was: ",guess)
outside of the function and executed after the function was called which always returned with the same value that the user inputs.
#Function to take guess
def userGuess (guess):
guess = input("Take a guess.\n")
print("Your guess was: ",guess)
return guess
#Define Variables
diceNum = random.randint(1,6)
guess = 0
#Call Function
guess = userGuess(guess)
#Check answer
if guess == diceNum:
print("You guessed right!, the number was: ",diceNum)
else:
print("You did not guess it right, the number was: ",diceNum)
Upvotes: 0
Views: 539
Reputation: 2301
You need to convert the user input to an integer prior to comparing:
guess = int(input("Take a guess.\n"))
If you want an explanation as to why your if statement returned false
for a comparison between an integer and a string, take a look at this question.
Upvotes: 2