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

Dmitriy Ignatiev
PLUS
Dmitriy Ignatiev
Courses Plus Student 6,236 Points

doesn't appear letter in good_guesses in my simple script

Hi, could't understand why it is not show the letter if letter in good_guesses.

Please help me to understand what is wrong with my code below.

Thank you in advance

    while True:

        good_guesses = []
        bad_guesses = []

        word = "secret"

        for letter in word:

            if letter in good_guesses:
                print(letter, end = ' ')
            else:
                print("_", end=' ')

        guess = input (">")


        if guess in word:
            good_guesses.append(guess)
        else:
            bad_guesses.append(guess)

        if guess == 'n':
           break

4 Answers

Hi there,

If you change the order of this, it works a little better. With your order, you're initially testing if letter in good_guesses when good_guesses is empty! So, do the adding to the two lists first, then loop through a non-empty array.

You need to refine how the dashes and letters are displayed as that's not quite right. The game never ends at the moment.

Here's the code - I just changed the order a little.

while True:

        good_guesses = []
        bad_guesses = []

        word = "secret"

        guess = input (">")

        if guess in word:
            good_guesses.append(guess)
        else:
            bad_guesses.append(guess)

        for letter in word:

            if letter in good_guesses:
                print(letter, end = ' ')
            else:
                print("_", end=' ')


        if guess == 'n':
           break
Dmitriy Ignatiev
PLUS
Dmitriy Ignatiev
Courses Plus Student 6,236 Points

Hi, Steve,

Many thanks for your help.

But i codlin't imaging how i could write code in order to appear all letters that i guess int one line, could you pls advise how it can be solved.

Thanks

For a starter, consider what is happening here.

You have a constant loop; the code repeats again and again. At each pass, all your code executes. Including:

good_guesses = []
bad_guesses = []

So you set your lists to empty every time. I'd suggest moving code you only want to execute once to be placed before the while True: line. That way, you're not resetting your lists.

I'd put the three variables above the while as they don't need stating at every loop:

good_guesses = []
bad_guesses = []
word = "secret"

while True:

I hope that helps.

Steve.