Loop breaks without executing the last print statement

Viewed 70

I've written a hangman program in python. The program is finished, but I have a problem with my main function. If I set a win or loss, it no longer execute my print statements from before. This means that if I have to guess a word, for example, it doesn't fill in the last letter in the placeholder, it just breaks out the loop without filling in the last letter. The same goes with my hangman. If I don't have any more attempts left, it won't finish drawing my hangman (just leave out the last part). Who knows why that is? Please help

Here is my code:

##### HANGMAN IN PYTHON #####

count_attempts = 0
guess_word = []

def guidance():
    print('Welcome to Hangman in Python!')
    print('''The point here is to guess a word by trying out letter combinations and 
defining them with ENTER.''')
    print('''For each word, you have 10 tries! If you can not solve the word, 
the game is over.''')
    print('You have 9 attempts to guess the word.')
    print('Have fun!')
    print('')


def user_word():
    userinput = str(input("Type in the word you want to be guessed: "))
    for character in userinput:
        if character.isdigit():
            print('There is a number in your word!')
            userinput = str(input('Please try again: '))
    return userinput.upper()

def change(userinput):
    global guess_word
    list_WordToBeGuessed = list(userinput)
    for characters in list_WordToBeGuessed:
        guess_word.append(' _')

def play_game(userinput):
    global count_attempts
    length_word = len(userinput)
    user_guessed = []
    print('Word to guess: ', *guess_word)
    print("")
    list_WordToBeGuessed = list(userinput)
    guess = str(input('Guess a letter: ')).upper()
    for number in guess:
        if number.isdigit():
            guess = str(input('Input not valid. Guess a letter: ')).upper()
    if guess in user_guessed:
        print('You have already given this letter. ')
    elif guess in list_WordToBeGuessed: 
        user_guessed.append(guess)   
        print("You've guessed a letter correctly!")
        for i in range(0, length_word):
            if list_WordToBeGuessed[i] == guess:
                guess_word[i] = guess
    else:
        count_attempts += 1
        user_guessed.append(guess)
        print('Sry your given letter is not in the word!')
    print("You have failed", count_attempts, "of 5 attempts")
            

def draw_hangman(count_attempts):
    print('-------' )
    print(' |     ' + ('|' if count_attempts > 0 else ''))
    print(' |     ' + ('O' if count_attempts > 1 else ''))
    print(' |    ' + ('/ \\' if count_attempts > 2 else ''))
    print(' |     ' + ('|' if count_attempts > 3 else ''))
    print('---   ' + ('/ \\' if count_attempts > 4 else ''))
    print("")


def main():
    guidance()
    userinput = user_word()
    change(userinput)
    while True:
        draw_hangman(count_attempts)
        play_game(userinput)
        if count_attempts >= 5:
            print("You haven't any attempts left. Bad luck")
            break
        elif ' _' not in guess_word:
            print("")
            print('You won, congratulation!')
            break     
main()
2 Answers

you are breaking out of a loop before it can draw the hangman and word.

in the endgame conditions, you can simply call draw_hangman(count_atttempts) before each print statement and change(userinput) as well.

Edit:

You will have to redraw the word as well. Since this is built into your play_game() function it is difficult, but you can use

        for i in guess_word:
            guess = guess + i + " "
        print(guess)

before the print statements along with the draw_hangman(count_attempts) change you made before. Please note you will need to create a new variable called guess in your main() function prior to adding this code.

Since you have a "break" in your endgame condition, the loop stops and last "draw_hangman(count_attempts)" is not called. You can simply add "draw_hangman(count_attempts)" before "print("You haven't any attempts left. Bad luck")"

Related