简体   繁体   中英

Python3 Try/Except Block

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

You forgot to save the value that time ( guess = int(guess) ), you're not return ing 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM