0

I'm new to python so any constructive criticism will be welcome. But when I type letters into the input it comes up with and error, but if I leave the input as input I get the error

unorderable types: str() >= int().

Is there any ways around this?

My code:

print("Please guess a number between 1 and 50")
import random
randomNumber = random.randint(1,50)

def main():
    banana = False
    while not banana: 
        userGuess = int(input("Your guess: ")) 
        print("Your guess was: {}".format(userGuess))
        if userGuess == randomNumber:
            print("Congrats!")
            banana = True

        elif userGuess >= 51:
            print("Please guess a number between 1 and 50")

        elif userGuess < randomNumber:
            print("Go higher!")

        elif userGuess > randomNumber:
            print("Go lower!")

if __name__ == "__main__":
    main()

print("Thank you for playing!")
timgeb
  • 76,762
  • 20
  • 123
  • 145
  • Your code is throwing an exception, as you don't handle it this exits the program. Read https://docs.python.org/2/tutorial/errors.html to get started... – l4mpi Jun 22 '14 at 17:41
  • Include the full traceback of your error. It contains important information, such as the specific lines in your program where the error(s) occurred. – Joel Cornett Jun 22 '14 at 17:42
  • This question is too broad? It seems like people have easily came up with the exact answers I needed and they all provided the same answer? @l4mpi – user3765181 Jun 22 '14 at 18:01
  • @user3765181 it's too broad because you don't even seem to know the basics of exceptions in python. An explanation (not just a "try this" code dump which works on this specific code, which does not constitute a good answer) would need to be at least as long as the linked tutorial and thus be too long for SO. This is stated in the close reason above. As shown by your comments on the accepted answer, the answer is far from enough to let you understand this topic; you don't need a short answer in a few paragraphs or comments, but an in-depth explanation like the tutorial I linked. – l4mpi Jun 22 '14 at 18:09
  • You will probably find [this question](http://stackoverflow.com/q/23294658/3001761) useful. – jonrsharpe Jun 22 '14 at 18:33

3 Answers3

1

Try using the try/except syntax.

This will 'try' to execute your code, then catch an exception you specify and perform some other code. Here is an example.

try:
  int(raw_input("Please enter a number!"))
except ValueError:
  print "That's not a number!"

It's worth noting that since you're using input instead of raw_input (which is probably a better idea anyway...), a NameError is the exception that is thrown, not a ValueError. You can see in the exception that comes up which exception you should be catching in the block.

If you want to continue the loop after responding to the error, simply add a 'continue' statement after the print. The following should compile in python3 (I'm a python2.7 user for the most part myself...)

try:
  int(input("Please enter a number!"))
except NameError:
  print("That's not a number!")
  continue

Adding that fixed the issue on my machine with your script.

The Velcromancer
  • 437
  • 3
  • 10
  • Might want to change `raw_input` to `input` as the OP is using Python3. – timgeb Jun 22 '14 at 17:47
  • Okay thanks for this answer, this is exactly what I am looking for. The ValueError is the main thing. What is the difference between Value Error and NameError – user3765181 Jun 22 '14 at 17:51
  • According to [the python docs](https://docs.python.org/2/library/exceptions.html), NameError is raised when a local or global variable is not found; ValueError is raised when a function receives input it can't handle. One would expect int(input()) to raise a ValueError; I'm not sure why it raised a NameError, to be honest. Maybe someone else has some insight there? – The Velcromancer Jun 22 '14 at 17:56
  • But when I run the code it comes out with this error: userGuess = int(input("Your guess: ")) ValueError: invalid literal for int() with base 10: 'zdjz' So does this not make it a value error? – user3765181 Jun 22 '14 at 17:59
0

You are getting an exception:

>>> int('asdf')

Traceback (most recent call last): File "", line 1, in int('asdf') ValueError: invalid literal for int() with base 10: 'asdf'

Because your program does not handle the exception, it stops execution.

The way around it is to use Python's exception handling, e.g. try/except syntax.

Alex W
  • 37,233
  • 13
  • 109
  • 109
0

Is there any ways around this?

Just query the user for input until he/she manages to put in an integer:

while True:
    try:
        userGuess = int(input("Your guess: "))
        break
    except ValueError:
        print('you did not input an integer!')
timgeb
  • 76,762
  • 20
  • 123
  • 145