0
votes

Can someone please help me understand the following issue...

I'm having problems executing my try/except block in my simple number guessing game. The function containing my error handling works fine if I remove the integer portion of the initial input. But if I do this, the rest of the game doesn't work, because from my understanding Python3 takes input and stores it as a string. So how can I get my exception to execute? Any help is much appreciated.

Thanks,

# number game
import random




print ("Welcome to the guessing number game!\n\n")

winning_number= random.randrange(1, 11)
guess = int(input("Can you guess the lucky number.\nHint it's between 1 and 10!\n"))


def is_number(guess):
    try:
        int(guess)
    except ValueError:
        print ('You need to type a number\n')
        guess = int((input("Please input a number\n")))
        game(guess)



def compare(guess):
    if guess > winning_number:
        print ("Wrong, you're guess is too high.\n")
        guess = int(input("Guess againn\n"))
        game(guess)
    else:
        print ("Wrong, you're guess is too low.\n")
        guess = int(input("Guess again\n"))
        game(guess)



def game(guess):
    is_number(guess)
    if guess == winning_number:
        print ("You win!, You guessed the number!")
    else:
        compare(guess)

game(guess)

Here is what I get when I input anything other than an integer...

Welcome to the guessing number game!

Can you guess the lucky number. Hint it's between 1 and 10! f Traceback (most recent call last): File "C:/Users/mickyj209/PycharmProjects/Practice/NumberGuess.py", line 10, in guess = int(input("Can you guess the lucky number.\nHint it's between 1 and 10!\n")) ValueError: invalid literal for int() with base 10: 'f'

Process finished with exit code 1

1
What's the issue you're having? - Morgan Thrapp
If the initial input is anything other than an integer the except portion of my function does not execute like it should. I still get a ValueError. - MJ49
Your game seems fine, share us the input that causes the problems - Timo
Protip for the future: random.randint makes more sense for assigning winning_number. - Alyssa Haroldsen
Adding my error to the post.... - MJ49

1 Answers

0
votes

You forgot to save the value that time (guess = int(guess)), you're not returning anything there, and you're just having the program run the function but not making a decision based on the result. You also have an int(input(... in the exception handling, which could itself generate an exception which won't be caught. The initial guess isn't in a try block, either.

You could refactor this program:

def game():

    print ("Welcome to the guessing number game!\n\n")

    winning_number = random.randrange(1, 11)
    print("Can you guess the lucky number?\nHint: it's between 1 and 10!\n")
    while 1:
        try:
            guess = int(input("Please input a number\n"))
        except ValueError:
            continue
        if guess > winning_number:
            print('Wrong - your guess is too high.')
        elif guess < winning_number:
            print('Wrong - your guess is too low.')
        else:
            print('You win! You guessed the number!')
            break