0

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)
1
  • 5
    guess is a string. diceNum is an int. Convert one or the other. Commented Oct 9, 2017 at 11:42

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.