Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

Python Letter Game

I generated the script and it runs, but it will continue running even after the correct word has been guessed. Not sure why the it will not terminate.

import os
import random
import sys

words = [
    'apple',
    'lemon',
    'pineapple',
    'banna',
    'strawberry',
    'grapefruit',
    'snikcers',
    ]
def clear():
    if os.name == 'nt':
        os.system(cls)
    else:
        os.system('clear')

def draw(secretWord, goodGuesses, wrongGuesses):
    clear()
    print('Strikes: {}/7'.format(len(wrongGuesses)))
    print('')
    for letter in wrongGuesses:
        print(letter, end=' ')
    print('\n\n')

    for letter in secretWord:
        if letter in goodGuesses:
            print(letter, end='')
        else:
            print('_', end='')
    print('')

def getGuess(wrongGuesses, goodGuesses):
    while True:
        guess = input("Guess a letter: ").lower()
        if len(guess) !=1:
            print ("you can only guess a single letter!")
            continue
        elif guess in wrongGuesses or guess in goodGuesses:
            print("you've already guessed that stuff!")
        elif not guess.isalpha():
            print("You can only guess letters!")
            continue
        else:
            return guess

def play(done):
    clear()
    secretWord = random.choice(words)
    wrongGuesses = []
    goodGuesses = []

    while True:
        draw(secretWord, goodGuesses, wrongGuesses)
        guess = getGuess(wrongGuesses, goodGuesses)

        if guess in secretWord:
            goodGuesses.append(guess)
            found = True
            for letter in secretWord:
                if letter not in goodGuesses:
                    found = False
            if found:
                print("You win!")
                print("The secret word was {}.".format(secretWord))
        else:
            wrongGuesses.append(guess)
            if len(wrongGuesses) == 7:
                draw(secretWord, goodGuesses, wrongGuesses)
                print("You lost!")
                print("The secret word was {}".format(secretWord))
                done = True
        if done:
            play_again=input("Play again? y/n").lower()
            if play_again != 'n':
                return play(done=False)
            else:
                sys.exit()
def welcome():
    start = input("Press enter to start or Q to quit.").lower()
    if start == 'q':
        print("bye!")
        sys.exit()
    else:
        return True


print ("Welcome to letter guess!")

done = False

while True:
    clear()
    welcome()
    play(done)

[MOD: added ```python markdown formatting -cf]

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,106 Points

You are missing setting done if word found:

            if found:
                print("You win!")
                print("The secret word was {}.".format(secretWord))
                done = True  # <- added setting 'done'