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 Basics (2015) Letter Game App Letter Game Introduction

Aboudi ABU SAMRA
Aboudi ABU SAMRA
1,000 Points

So confused

I start the script and when I press any letter it prints forever

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 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 spaces

        # draw guessed letters and stricks
        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

    #print out win/lose

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

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,167 Points

Currently, the second while statement has no exit. Add more code to ask for guesses thus the bad_guesses will break out of the loop eventually.

This is the danger of coding while loops: make sure the the loop exit code is understood or programmed first.

Post back if you have more questions. Good Luck!!!

Chris Freeman
Chris Freeman
Treehouse Moderator 68,167 Points

It's not necessarily a literal "exit". Rather, it is either a break statement or something that will eventually cause the while conditions to fail.

In extreme debug situations, due to code that has a lot of code yet to be implemented, I'll add counter to limit the while loops. A sort of "override". Once the code is working the override safety breakout can be removed.

A simple way to add this:

# before while loop
count = 10  # some limit

# while loop
while conditions and count > 0 # count is positive 
    count -= 1  # decrement count