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 trialryan smith
687 PointsWhy can't guesses.append(guess) be above line 22?
Why can't guesses.append(guess) be above line 22?
So at 4:25 Kenneth talked about not putting the
guesses.append(guess)
because if the user got it right on the fifth try it would end the loop before they guessed it. I'm confused about how that is possible, the code hypothetically in my mind seems fine above line 22 (see below). Kenneth is right the loop would end, but I don't know why
#it goes here
if guess == secret_num:
4 Answers
ds1
7,627 PointsHi, Ryan
Good question. I'm not nearly as knowledgeable as Kenneth, but from what I can tell by only looking at the bit of code that's viewable at 4:25, I don't see a technical problem with putting the append statement before the if statement. This is because a program doesn't exit a while loop immediately after the while loop's condition is false... it exits the while loop when the condition is re-evaluated (when it loops back up to the top). If this wasn't the case, you'd never see the print statement outputs below when "c" has a len of > 0 when running this example:
def c_len():
while len(c) == 0:
print(len(c))
c.append(1)
print(len(c))
c.append(2)
print(len(c))
c.append(3)
print(len(c))
print('the loop is already over!')
print('last output of function')
c = [] # empty list
c_len() # call function
However, maybe Kenneth is considering something else in the entire program that I can't see. I'd be interested to hear Chris Freeman 's thoughts on this... he's quite knowledgeable too!
Chris Freeman
Treehouse Moderator 68,454 PointsIn short, I think Kenneth Love misspoke. The append can be at line 19 or later without affecting the while loop.
As ds1 mentioned, the while loop is only evaluated at the start of each loop. The append to guesses
can not have an effect until the next loop starts.
As Steve mentioned, perhaps Kenneth intends to keep good guesses and bad guess separated (as will be needed in a later challenge), but here there isn't an explicit reason in this code to separate them.
Kenneth is on sabbatical so it maybe sometime for him to respond to this question.
ds1
7,627 PointsThanks, Chris!
Steven Parker
231,271 PointsDid you actually try this in the workspace? I don't see how it would make any difference at all (to making the list stop) if you do it before or after the test.
There would be one other difference. If the code is before, the correct answer will go into the list, but if you put it after, the list will only contain the wrong answers. But the loop would end either way on a correct answer because of the break
ryan smith
687 PointsBut he said "it would end the loop before they guessed it" if done before the code. I'm talking specifically about that, what does he mean by that? I know it will break no matter what.
Steven Parker
231,271 PointsI checked the video and I really can't explain what he is saying there. It doesn't seem to relate to how the loop would perform. But I will tag him here and perhaps he will comment when he's available.
Please enlighten us, Kenneth Love !
Kenneth Love
Treehouse Guest TeacherIt's been awhile, but, yeah, likely just a mistake on my part. Regardless, it's good to think about when/where you're doing your mutations so that you don't get unexpected side effects.