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 Refinement

Henry Lin
Henry Lin
11,636 Points

Why do we use while loop in get_guess() function?

I've tried without while loop in get_guess() function and it worked fine. I just don't see the point that we put while loop in side this function.

def get_guess(bad_guesses, good_guesses):
    while True:
        # take guess
        guess = input("Guess a letter: ").lower()
        if len(guess) != 1:
            print("You can only guess a single letter!")  
        elif guess in bad_guesses or guess in good_guesses:
            print("You've already guess that letter!") 
        elif not guess.isalpha():
            print("You can only guess letters!")
        else:
            return guess

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

The purpose of the while loop is to keep printing why an input was not accepted then re-ask for an input. Without the while loop, an unaccepted input would print the message then return None from the function call.

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