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

I'm still confused about the explanation for the def get_guess code block

I'm unclear on the explanation for the code block

def get_guess(bad_guesses, good_guesses):
    while True:
        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 guessed that letter!")
        elif not guess.isalpha():
            print("You can only guess letters!")
        else:
            return guess
  1. Why add the while True? Is that purely just to run the block?
  2. He mentions you could take or leave the continues. If they don't add anything, why would you leave them? I guess that's why he removes them?

Thanks, Andrei

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

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

Good questions!

  1. Why add the while True? Is that purely just to run the block?

If any of the first three if conditions evaluate to True, a message will be printed and the if will end. The while will then run again (and again) until all three of those if conditions fail and the final else returns the guess

  1. He mentions you could take or leave the continues. If they don't add anything, why would you leave them? I guess that's why he removes them?

In more complicated code, leaving the continue statements might help make the code clearer to read because it's obvious the next loop would start immediately instead of having to read down the code block to find out nothing else would actually happen.

Thanks! So, without the <p>while</p> loop, one of the if conditions could run, evaluate to true and then it wouldn't necessarily move on to the next if statement? It would just end?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

Correct. With the if/elif/elif/else structure, when any if evaluates true, only it's small one-line code block executes, then the entire if statement is done. Without the while loop, the code falls to the end of the get_guesses function and it returns. Since it didn't hit the return statement in the last else code block, the function would default to a return None.

Scott Benton
Scott Benton
2,563 Points

Something else that isn't specifically mentioned, the while loop will stop ONLY if an acceptable guess is entered. Since the while loop is within a function, 'return guess' terminates the function's execution, including the while loop. You could add a 'break' just above the 'return guess', which would explicitly terminate the while loop, but since the return is going to terminate everything within that function's scope, it isn't necessary.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

Good point Scott. Keep in mind, if you add a break "just above the return guess" the return statement wouldn't execute unless it was unindented to be outside the while loop, that is, it would execute only after the while loop completes.

In some regards, especially as functions get more complex, moving the return outside the while loop is a more readable solution since the return is at the end of the function definition and not buried inside of another statement.

Scott Benton
Scott Benton
2,563 Points

Yeah, I missed that. Thanks for clarifying. I'm coming from PHP, where white space is meaningless, and all of the control structures are clearly market with curly brackets. I'm finding it harder to read where everything begins and ends in Python, but I guess I'll get used to it.