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)
Chanda Korat
  • 2,453
  • 2
  • 19
  • 23
Jake2018
  • 93
  • 1
  • 10
  • 5
    `guess` is a string. `diceNum` is an int. Convert one or the other. – khelwood Oct 09 '17 at 11:42
  • Possible duplicate of [How can I read inputs as integers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-integers) – SiHa Oct 09 '17 at 12:45

1 Answers1

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.

Georg Grab
  • 2,271
  • 1
  • 18
  • 28