-2

I'm trying to make a small program in Python that generates a random number (from the throw of a die) and asks the user to guess what the number is, and if the user guesses wrong, then depending on whether the guess is higher than the actual number or not the program is supposed to re-prompt them for another guess.

The problem that I'm having with the code is that the output is always "That's a little high. Try again!", even if I input all the possible values.

Can someone please help me figure out which portion of the code is generating the error?

import random

dice = random.randint(1,6)


answer = raw_input ("What do you think is the number?")
while (answer != dice):
    if answer > dice:
        print ("That's a little high. Try again!\n")
    else:
        print ("That's a little low. Try again!\n")
    answer = raw_input ("What do you think is the number?")

print ("That's correct! Good job. \n")

Thank you!

Emily
  • 29
  • 3
  • possible duplicate of [Asking the user for input until they give a valid response](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Morgan Thrapp Sep 10 '15 at 16:15
  • You need to convert `answer` to an `int`. – Matthias Sep 10 '15 at 16:16
  • 1
    Aside: it looks like you're using Python 2 (from `raw_input`) but putting parentheses in `print` (like you would for Python 3). I recommend upgrading to Python 3, which would have given you a helpful error message like `TypeError: unorderable types: int() < str()` or something which you could have then googled for help. – DSM Sep 10 '15 at 16:19

1 Answers1

2

raw_input returns a string, which is always bigger than a number from 1-6 as its numerical representation is taken in comparison. you need to cast the input to int

answer = int(raw_input ("What do you think is the number?"))
Lawrence Benson
  • 1,398
  • 1
  • 16
  • 33