简体   繁体   中英

How to catch a Python error?

I want a way I can catch error input from the user. I found Pythons try/except method.

I have this so far:

def take_a_guess(self):
    random_card_rank = random.choice(list(self.card.rank.keys()))
    random_card_suit = random.choice(self.card.suit)
    print(random_card_rank)
    print(random_card_suit)
    rank_guess, suit_guess = input('Guess the card: ').split()
    guesses = 3

    while guesses != 0:
        # try:
            if int(rank_guess) == random_card_rank and suit_guess.rstrip() == random_card_suit:
                print('Wow, well done you got it in one')
                guesses -= 3
            else:
                print('Nah')
                return False
            return True
        # except ValueError:
        #     print('You cant do that! Try again...')

but if I type in 1 thing to the input, it says ValueError: not enough values to unpack (expected 2, got 1) I get why but I thought that the except would catch this and tell me I cannot do this??

You need to surround the entire relevant piece of code in your try in order to catch the relevant error. Your error stems from this line:

rank_guess, suit_guess = input('Guess the card: ').split()

Which is not in your try block at all.
You should add your try anywhere before that line:

def take_a_guess(self):

    random_card_rank = random.choice(list(self.card.rank.keys()))
    random_card_suit = random.choice(self.card.suit)
    print(random_card_rank)
    print(random_card_suit)

    # try should be here at the very least (or higher)
    rank_guess, suit_guess = input('Guess the card: ').split()

    # catch could be here and it would catch the error as expected

    guesses = 3
    while guesses != 0:
        # try should not be here
            if int(rank_guess) == random_card_rank and suit_guess.rstrip() == random_card_suit:
                print('Wow, well done you got it in one')
                guesses -= 3
            else:
                print('Nah')
                return False
            return True
        # except ValueError:
        #     print('You cant do that! Try again...')

In order to keep looping if the error is raised you can extract that line into the while like so:

def take_a_guess(self):

    random_card_rank = random.choice(list(self.card.rank.keys()))
    random_card_suit = random.choice(self.card.suit)
    print(random_card_rank)
    print(random_card_suit)

    guesses = 3
    while guesses != 0:
        try:
            rank_guess, suit_guess = input('Guess the card: ').split()
        except ValueError:
            print('You cant do that! Try again...')
            continue

            if int(rank_guess) == random_card_rank and suit_guess.rstrip() == random_card_suit:
                print('Wow, well done you got it in one')
                guesses -= 3
            else:
                print('Nah')
                return False
            return True

What you want to try/catch is the

rank_guess, suit_guess = input('Guess the card: ').split()

You're trying to split something that you don't know yet.

Have you tried it?

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