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 trialA B
1,146 PointsI'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
- Why add the while True? Is that purely just to run the block?
- 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
Treehouse Moderator 68,457 PointsGood questions!
- 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
- 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.
Scott Benton
2,563 PointsYeah, 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.
A B
1,146 PointsA B
1,146 PointsThanks! 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
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsCorrect. With the if/elif/elif/else structure, when any
if
evaluates true, only it's small one-line code block executes, then the entireif
statement is done. Without thewhile
loop, the code falls to the end of theget_guesses
function and it returns. Since it didn't hit thereturn
statement in the lastelse
code block, the function would default to areturn None
.Scott Benton
2,563 PointsScott Benton
2,563 PointsSomething 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
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsGood point Scott. Keep in mind, if you add a break "just above the
return guess
" thereturn
statement wouldn't execute unless it was unindented to be outside thewhile
loop, that is, it would execute only after thewhile
loop completes.In some regards, especially as functions get more complex, moving the
return
outside thewhile
loop is a more readable solution since thereturn
is at the end of the function definition and not buried inside of another statement.