1

I have this code running in a number guessing game I have written, it runs perfectly fine if the player follows the instructions, but as we all know users never do. If the user enters just a space or any string thats not the word hint then it crashes saying invalid literal for int() with base 10: when trying to convert the value of guess to an integer. Is there any way around this or am I just going to have to live with the crashes?

while repeat==1:
    repeat=0
    level1number=str(level1number)
    guess=input("What is your guess? ")
    guess=guess.lower()
    if guess==level1number:
        print("Well done, You have guessed my number!")
    elif guess=="hint":
        print("Hints are not available until level 3")
        repeat=1
    elif guess!=level1number:
        print("Sorry that is not my number, you have lost a life. :(")
        lives=lives-1
        repeat=1
        if lives<=0:
            print("You have lost all your lives, so this means I win")
            print("The program will now end")
            exit()
            input("")
        level1number=int(level1number)
        guess=int(guess)
        if guess<level1number:
            print("The target number is higher")
        else:
            print("The target number is lower")
9
  • Catch the exception and keep asking a valid value. Commented Oct 20, 2016 at 9:16
  • There are many ways around this. The easiest is probably using a while loop and try except block to keep asking the user for input if it can't be converted to int. Commented Oct 20, 2016 at 9:16
  • it runs the not equal to section of the loop if its not the target number or the word hint, causing the crash Commented Oct 20, 2016 at 9:18
  • You shouldn't cast level1number to str and guess should be an integer as soon as possible. Commented Oct 20, 2016 at 9:18
  • 1
    guess the error should be coming from core "guess=int(guess)" Commented Oct 20, 2016 at 9:21

2 Answers 2

4

Use something as

if guess.isdigit() ...

(method isdigit() returns true if and only if all characters of a given string are digits, i.e. 0 to 9).

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

2 Comments

This is the best answer (so far); while it returns False then make it keep asking the user for his input again.
@MarianD Do you know of a signed version of this? For instance '-1'.isdigit() will return false but can evaluate to a number. '-1'.isnumeric() will also return false
1
while repeat==1:
    repeat=0
    level1number=str(level1number)
    guess=input("What is your guess? ")
    guess=guess.lower()
    if guess==level1number:
        print("Well done, You have guessed my number!")
    elif guess=="hint":
        print("Hints are not available until level 3")
        repeat=1
    elif guess!=level1number:
        print("Sorry that is not my number, you have lost a life. :(")
        lives=lives-1
        repeat=1
        if lives<=0:
            print("You have lost all your lives, so this means I win")
            print("The program will now end")
            exit()
            input("")
        level1number=int(level1number)
        try:
            guess=int(guess)
            if guess<level1number:
                print("The target number is higher")
            else:
                print("The target number is lower")
        except:
            print("Try again. Not a number")

Using try/except block would solve your problem. Have a look

Edit: In the question. you mentioned that you get an error when something other than a number is entered. Actually, it is an exception that is thrown when your code tries to convert your input string to a number when it is not possible(guess = int(guess)) due to the input not being a number, just like a space. So, what my code does, is that it catches the exception, and does not allow the program to terminate with the exception.

Just try it once. I know you are beginner but it is better to learn about exception handling as soon as possible, before you write more and more complex codes and applications.

Hope it helps!!

2 Comments

thanks, im not a professional and i have never heard of try/except
i have accepted the answer, just my reputation isnt high enough for it to be visible

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.