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

Ashton Holgate
Ashton Holgate
6,021 Points

Endless Loop In Code

This code was coped from the video 'Letter Game Introduction'

I'm not entirely sure why but, when running the program, it first says 'press enter/return to start, or enter Q to quit: ' as expected but then, if you press anything other than q, it runs an endless print of _

Why would this be? I can't quite work out why that's happening.

Thanks for any and all help!

import random

# make a list of words
words = [
    'apple',
    'banana',
    'orange',
    'coconut'
]

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)):
        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 guesses
    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 letter!")
        continue

    if guess in secret_words:
        good_guesses.append(guess)
        if len(good_guesses) == len(list(secret_word)):
            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))

    # draw guessed letters and strikes
    # print out win/lose```

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

The while loop code consists of only the print statements thus it has no way out of the while loop. The code below the while loop needs to be indented.

import random

# make a list of words
words = [
    'apple',
    'banana',
    'orange',
    'coconut'
]

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)):
        for letter in secret_word:
            if letter in good_guesses:
                print(letter, end='')
            else:
                print('_', end='')
        ### indent below here ###
        print('')
        print('strikes: {}/7'.format(len(bad_guesses)))
        print('')

        # take guesses
        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 letter!")
            continue

        if guess in secret_words:
            good_guesses.append(guess)
            if len(good_guesses) == len(list(secret_word)):
                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))

        # draw guessed letters and strikes
        # print out win/lose

You also may want to use set instead of list in:

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

to handle case of repeated letters in secret_word.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Hey Ashton, in my posted code the comment "indent below here" marks where I indented your code 4-spaces to correct it. The only difference from your code to my code is the indentation I made.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

And by "indent" I mean using 4-spaces to signify a code block.

Ashton Holgate
Ashton Holgate
6,021 Points

Thanks for the help Chris. I now understand!

Ashton Holgate
Ashton Holgate
6,021 Points

Thank you for the help. I'm not sure I fully understand what you mean by the necessary indent as I thought I had indented the code correctly.

Where you have put the comment ### indent below here ###, in my code, there is an indent. Do you mean there should be two indents?

I am quite certain I am misunderstanding what you are saying because I am misunderstanding what an indent is. In my code there are no spaces, should there be 8 for it to be called an 'indent'?

Thanks again for the help!

Marcin Mączewski
Marcin Mączewski
4,476 Points

Can someone help me with my code? I keep getting an endless loop as well, but in my case it keeps printing:

_ Strikes: 0/7 _ Strikes: 0/7 _ Strikes: 0/7

And so on. Here's my code:

import random

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

    secret_word = random.choice(words)
    bad_guesses = []
    good_guesses = []

    while len(bad_guesses) < 7 and len(good_guesses) != len(list(secret_word)):
        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('')

    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)):
            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))

Thanks!

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Marcin Mączewski advice: you'll get a better response asking in a new posting. Since this is already marked best answer few will click through to see it. This is only see by those already on the thread or this tracking active threads.