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

Xiaobao Yu
PLUS
Xiaobao Yu
Courses Plus Student 4,346 Points

This code has a bug.

If the secret_word has duplicate letters, then you can never get out of the loop, because your good_guesses length will always be shorter than the secret_word.

3 Answers

Jonathan Fernandes
PLUS
Jonathan Fernandes
Courses Plus Student 22,784 Points

It sure does (and I am glad you caught it!). Most code we create will sometimes present a bug from the way we build it. In the following videos he will mention it and show you how to correct it. Bugs will happen and will sometimes be your best teachers (some of the best tricks I have in my sleeve or from bugs I created and had to figure out how to fix).

As for fixing the bug, think about this: instead of checking to see if the length of your good guesses match the length of the secret word, try changing it to see if all the letters are in good guesses. For example, you could do the following in a while loop:

winning_condition = True
for letter in secret_word:
    if letter not in good_guesses:
        winning_condition = False
if winning_condition:
    # Write what you want your code to do if this is true
else:
    # Write the losing actions here

I hope that makes sense and happy coding!

Michaล‚ Borowiecki
Michaล‚ Borowiecki
14,097 Points

Here's my solution. Delete the length comparing part and paste it there instead. Works like a charm!

The code counts how many times each correctly guessed letter appears in the secret word. The word is guessed if the sum of all counts equals the length of the secret word.

        if guess in secret_word:
            good_guesses.append(guess)
            #HERE STARTS MY CODE
            counter = 0
            for letter in secret_word:
                if letter in good_guesses:
                    counter += 1
            if counter == len(secret_word):                    
                print("You win! The word was {}".format(secret_word))
                break
Nicky van Bottenburg
Nicky van Bottenburg
4,646 Points

You could also use len(set(secret_word)) instead of len(list(secret_word)), right? The set() will make sure there are no double letter in the list.

Another bug that I found was the once a correct letter was guessed, i could continue to select that letter 'o' in 'orange' for example. I would select 'o' until the len(good_guesses) was equal to len(list(secret_word)) and I would win the game only needing to know 1 correct letter. I changed the len(list(good_guesses)) and len(list(secret_word)) in the example to set(good_guesses) and set(secret_word) without len and both issues were corrected.