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

Kevin Faust
Kevin Faust
15,353 Points

Why do we change secret_word to a list before getting its length + other questions

We did

len(good_guesses) != len(list(secret_word))

where good_guesses is a list and secret_word is a string. Was changing it to a list necessary?

We also did this

 while len(bad_guesses) < 7 and len(good_guesses) != len(list(secret_word)):

but I dont think the second part was necessary. because if the lengths equal each other our while loop would automatically break when we do almost exactly the same thing near the end of the code. I think kenneth just needed a way to introduce the keyword and to us

Lastly. when i guess all the correct letters, the win message that is supposed to be printed near the end of the code never gets run. i dont know why this is happening

Code from the video:

import random

# make a list of words
words = [
    'apple',
    'banana',
    'orange',
    'coconut',
    'strawberry',
    'lime',
    'grapefruit',
    'lemon',
    'kumquat',
    'blueberry',
    'melon'
]

while True:
    start = input("Press enter/return to start, or enter Q to quit")
    if start.lower() =='q':
        break

    # pick a random word
    secret_word = random.choice(words)
    bad_guesses = []
    good_guesses = []

    while len(bad_guesses) < 7 and len(good_guesses) != len(list(secret_word)):
        # draw guessed letters, spaces 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

        if guess in secret_word:
            good_guesses.append(guess)
            if len(good_guesses) == len(list(secret_word)):  # would automatically break here 
                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

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

For any string, len(string) == len(list(string)), so it's not clear why the string is converted to a list.

On your second point, I also can not think of a scenario where the second condition in the while would be trigger since, as you say, the same condition would be caught inside the loop and cause a break before the while loop circles back again:

    while len(bad_guesses) < 7 and len(good_guesses) != len(list(secret_word)):
    # ...
        if guess in secret_word:
            good_guesses.append(guess)
            if len(good_guesses) == len(list(secret_word)):  # would automatically break here 
                print("You win! The word was {}".format(secret_word))
                break

As for why the print "You win!" not happening, it seems that if the secret_word has a repeating letter, then the number of good_guesses could never be the same as the number of letters in secret_word.

A better architecture of the solution would be to make a set out of the secret_word characters. Then the good_guess length would match the set length at game completion.

In fact, the game fails to recognize a win on a word when there is a repeated letter.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Keep going! We fix this in the next video.