Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (2015) Letter Game App Letter Game Introduction

Jesse Kroon
Jesse Kroon
8,275 Points

Game doesn't display 'win' message if the word has been guessed.

When all the letters in the word have been guessed, the game just continues without displaying the 'you win' message.

Here's my code(game loop).

    while len(bad_guesses) < 7 and len (good_guesses) != len(list(secret_word)):
        # draw spaces, guessed letters and strikes
        for letter in secret_word:
            if letter in good_guesses:
                print(letter, end='')
            else:
                print('_', end='')

        print('')
        print('Strikes: {}/7'.format(len(bad_guesses)))
        print('')

        # take guess  
        guess = input("Guess a letter: ").lower()

        if len(guess) != 1:
            print("You can only guess a single letter!")
            continue
        elif guess in bad_guesses or guess in good_guesses:
            print("You've already guessed that letter!")
            continue
        elif not guess.isalpha():
            print("You can only guess letters!")
            continue

        # print out win/lose    
        if guess in secret_word:
            good_guesses.append(guess)
            if len(good_guesses) == len(list(secret_word)):
                print("You win! The word was {}".format(secret_word))
                break
        else:
            bad_guesses.append(guess)
    else:
        print("You didn't guess it. My secret word was {}".format(secret_word))

1 Answer

Steven Parker
Steven Parker
231,269 Points

You might need a different strategy for detecting a win.

It looks like you determine a win based on the number of guesses being the same as the word length, but this will only work if all the letters in the word are different. If the word has any duplicated letters, you will win with fewer guesses than the word length. For example, the word "banana" has 6 letters, but requires only 3 guesses to win (a, b and n).

Give this some thought, and see if you can think of a way to tell when you win if the word has duplicated letters, then change that part of the program to implement your new strategy.

Jesse Kroon
Jesse Kroon
8,275 Points

Thanks for the answer! I get where you are going, and I get what the line does. Didn't see that before! However, I have been crunching my brains on how to fix/work around that double letter problem, but I can't seem to find a solution. Maybe I am thinking too complex. Could you give any lead/tip?

Steven Parker
Steven Parker
231,269 Points

I'm sure there's several methods that would work, but one that comes to mind is to check to see if every letter of of the secret_word is also in the good_guesses list. If so, you have won, no matter how many there are.

Lois Shedd
Lois Shedd
1,126 Points

I had this problem, too. In a way I'm glad, though, because the idea that the length of the word and the number of good guesses needed to be the same was bugging me while I was writing it - now I know why!