Python Python Basics (2015) Number Game App Number Game Refinement

Michael Nock
Michael Nock
Android Development Techdegree Graduate 16,017 Points

Reason for putting "guesses.append(guess)" at the end.

In the video Kenneth mentions that we have to put it at the end of the While loop, after we check if a player has guessed correctly or not. However, the while loop condition is only checked once it's reached the end of the loop, so it really shouldn't matter where you put the append. Even if you put it first it'll run through the rest of the code. Having to interrupt the code to evaluate "len(guesses) < 5" after every single instruction would be much more processor intensive ;)

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 58,942 Points

You are correct in that there is some leeway in where the append can be placed. Anywhere within the else block of the try statement works. This makes sure the guess was valid before appending it.

Not sure about your "evaluate... after every single instruction" comment. The while condition is only evaluated once before the start of each loop.

Michael Nock
Michael Nock
Android Development Techdegree Graduate 16,017 Points

Not sure about your "evaluate... after every single instruction" comment. The while condition is only evaluated once before the start of each loop.

That's what i was saying, that there's no listener or anything attached looking for changes to the guesses list, it only evaluates the condition once per loop (at the start). :)