Search code examples
pythoncontrol-flow

Python - Hangman


I just wrote a functioning program to play Hangman game, but there's one issue that's bothering me which I haven't been able to figure out.

The program correctly asseses whether the chosen character is in the word to be guessed, as well as its position in the word (even if the character appears more than once). Nonetheless, when playing the game it takes one more turn than needed to asses whether you guessed the word completely, or whether you have no lifes left.

Say, for example, you have to guess the word 'hello'. I would expect to program to run like:

Turn 1: 'h'. Turn 2: 'e'. Turn 3: 'l'. Turn 4: 'o'. The program should stop here, but it needs another character (any) to recognize that the game has been completed.

Here's my code:

def play_game():

    # Pick word randomly from list
    random_num = random.randint(0, (len(words_list) - 1))
    current_word = list(words_list[random_num].upper())
    print(current_word)
    print('The word has', str(len(current_word)), 'characters')

    current_letter = input('Pick a letter: ').upper() #User input
    picked_letters = []
    correct_ones = []
    for i in range(len(current_word)):
        correct_ones.append('_ ')

    lifes = 3
    while lifes != 0:

        # Check if all characters have been already guessed
        if current_word == correct_ones:
            print('You won!')
            break
    
        # Check if character has already been chosen
        elif current_letter in picked_letters:
            print('You already chose this letter!')
            current_letter = input('Pick another letter: ').upper()
            continue
    
        # Check if character is in word
        if current_letter in current_word:
            index_list = []
            for i in range(len(current_word)): #Get indexes of character in word
                if current_word[i] == current_letter:
                    index_list.append(i)
                       
            picked_letters.append(current_letter) #Append to keep track of chosen characters         
            for i in index_list:
                correct_ones[i] = current_letter.upper() #Append to correct position
        
            print('Correct!')
            for i in correct_ones:
                print(i + ' ', end='')
            current_letter = input('Pick another letter: ').upper()
                
        # Incorrect character
        else:
            picked_letters.append(current_letter)
            lifes -= 1
            print('Incorrect')
            print('You have', str(lifes), 'lifes left')
            current_letter = input('Pick another letter: ').upper()
            continue
 

play_game()

If you see any bad practice in my code feel free to tell me so!

Thanks in advance!


Solution

  • You ask the user for input at the end of your loop. So when you enter the last correct character, you ask the user for input again:

    print('Correct!')
    for i in correct_ones:
        print(i + ' ', end='')
    current_letter = input('Pick another letter: ').upper()
    

    To fix this, you can move the check you do at the beginning of you loop to right there. Since at the start of the game, no input has been given so the word should never be correctly guessed (except if the true word is empty but that should not happen!). And because only guessing a correct character can get you the win, checking for it makes sense there. Hope this helps, have fun :)