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

alright, im doing this letter game, but the while loop doesnt seem to close itself when conditions are meant...

import random
import time
import os

def clear():
    os.system('cls')

def pause():
    time.sleep(1)

words = [
    'teacher',
    'student',
    'class',
    'desk',
    'board',
    'pencil',
    'paper',
]

while True:
    start = raw_input('ENTER to start Q to quit: ').lower()
    if start == 'q':
        break
    secretword = random.choice(list(words))
    badguesses = []
    goodguesses = []

    while len(badguesses) < 7 and len(goodguesses) != len(secretword):
        clear()
        for letter in secretword:
            if letter in goodguesses:
                print letter,
            else:
                print '_',

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

        guess = raw_input('Guess a letter: ').lower()

        if len(guess) != 1:
            print 'You can only guess one letter'
            pause()
            continue
        elif guess in badguesses or guess in goodguesses:
            print 'You already guessed that'
            pause()
            continue
        elif not guess.isalpha():
            print 'You can only guess letters'
            pause()
            continue
        elif guess in secretword:
            goodguesses.append(guess)
        else:
            badguesses.append(guess)
    else:
        print 'You didn\'t guess it! My secret word was {}'.format(secretword)

Have you already finished the course? I think I can remember that the version you are building during the course, has some issues until the end, when the code is re-structured.

1 Answer

and len(goodguesses) != len(secretword) - this part tricked me too a week ago, and it turned out that most words have multiple letters that shows more than once, i.e. treehouse has 3x 'e' but there will only be 1x 'e' in goodguesses.

Instead you could try check if there are any _ left in secret_word

I hope this makes sence? :-)

ah! i understand the issue now! although I dont completely understand the solution, but now that I actually know whats wrong, i can figure out the rest. thanks!