简体   繁体   中英

Hangman Code Getting stuck on try/except block

Sorry guys, I'm basically a baby coder. When I try to run this Python hangman program, it just says "This is not a valid input" like what's in the except box. Can y'all please let me know what I'm doing wrong?

import random

print("Welcome to Hangman for Python! A word will be chosen randomly. You will have to guess it!\n")

Hangman Variable:

    HANGMAN = (
    """
    -----
    |   |
    |
    |
    |
    |
    |
    |
    |
    --------
    """,
    """
    -----
    |   |
    |   0
    |
    |
    |
    |
    |
    |
    --------
    """,
    """
    -----
    |   |
    |   0
    |  -+-
    |
    |
    |
    |
    |
    --------
    """,
    """
    -----
    |   |
    |   0
    | /-+-
    |
    |
    |
    |
    |
    --------
    """,
    """
    -----
    |   |
    |   0
    | /-+-\ 
    |
    |
    |
    |
    |
    --------
    """,
    """
    -----
    |   |
    |   0
    | /-+-\ 
    |   | 
    |
    |
    |
    |
    --------
    """,
    """
    -----
    |   |
    |   0
    | /-+-\ 
    |   | 
    |   | 
    |
    |
    |
    --------
    """,
    """
    -----
    |   |
    |   0
    | /-+-\ 
    |   | 
    |   | 
    |  |
    |
    |
    --------
    """,
    """
    -----
    |   |
    |   0
    | /-+-\ 
    |   | 
    |   | 
    |  | 
    |  | 
    |
    --------
    """,
    """
    -----
    |   |
    |   0
    | /-+-\ 
    |   | 
    |   | 
    |  | | 
    |  | 
    |
    --------
    """,
    """
    -----
    |   |
    |   0
    | /-+-\ 
    |   | 
    |   | 
    |  | | 
    |  | | 
    |
    --------
    """)

Code:

life = len(HANGMAN) - 1
words = ["theeverythingstore"]
word = words[random.randint(0, len(words) - 1)]
guess = list(len(word) * '*')

letters_guessed = []

print(HANGMAN[0])

while list.count(guess, "*") > 0 and life != 0:
    guess_joined = "".join(guess)
    print("You have {0} lives left. The word so far is 
{1}.".format(life, guess_joined))
    try:
        char = input("\nEnter a letter: ")
    except:
        print("This is not a valid input")
    else:
        if len(char) > 1 or len(char) == 0:
            print("This has more than one letter or none. Try again!")
            continue
        elif char.isalpha() == False:
            print("I need a letter from a to z.")
            continue
        else:
            if char in letters_guessed:
                print("You already entered that letter!")
                continue
            else:
                char = char.lower()
                letters_guessed.append(char)

                for letter in range(len(word)):
                    if char == word[letter]:
                        guess[letter] = char

                if char not in word:
                    life -= 1
                    print(HANGMAN[(len(HANGMAN) - 1) - life])

                if '*' not in guess:
                    print("Congratulations! {} was the word!".format(word))
                    break
                if life == 0:
                    print("Unlucky, the word was {}. Try next time".format(word))
                    break

Actual result:

You have 10 lives left. The word so far is ******************.

Enter a letter: e This is not a valid input You have 10 lives left. The word so far is ******************.

Enter a letter:

A catch-all except like that is generally bad, as it puts you exactly on the kind of situation you're in right now: something is failing, but you don't know what.

First thing you can do is to remove (or just comment out) the except block, and see what happens. Python will raise the exception and end the program (as there is no exception handling, anymore). While that looks bad, Python will also print the details of the error, which will help you understanding what's going on.

After you do that, you may want to replace the block by something like this:

except Exception as e:
    print ("Invalid input (%s)" % e)

That will capture (almost) all exceptions you were previously catching, but it will also give you something to work with (an Exception name)

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