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

Can someone help me understand this code please?

https://w.trhou.se/lpkwhpzpsg

That's a snapshot above, i'm having trouble with Letter_game2.py

So i just want to understand the parts i'm going to ask about, because i got confused and don't want to move onto the next section until i understand them.

line 18 - 22,

i understand that name and system, the net is for windows and clr is to clear the screen and the else is there in case it is mac or something else, so either way it clears the screen, so is that the main reason for this function, that is clears my screen after typing information and then whatever need to be retained stays on the appropriate section of the screen?

line 45, the function there that has (good, bad) for arguments, what does good and bad do, i'm confused with how arguments work honestly. So here couldn't that part of code have been written without them or did it need them to write the code?

then line 58, what does return guess do ? Couldn't we have done print guess? I'm still trying to understand how return works as well.

then line 71,

what in the world is found = True for? I'm very confused here, as well as line 74 where found is now false? Then line 75 where, if found: what does this mean? that whole part of code from line 71 to 75 has me lost.

then line 83 where done = true and that is the argument to the play function? What does that do? then line 89 where return makes done = false? Return runs the play function and makes the argument false??? how did we run the function through return that also confuses me?

then line 99 what does return true do there? Why is it there?

then lastly what does 103 done = False do, how did done go from true to false and what did it do each time?

I really appreciate any help here, I understand most the code but i feel this will really help me get clarity. Thank you so much to all who have helped me so far and continue to help me, couldn't do it without you!!

josephr
josephr
18,877 Points

"line 45, the function there that has (good, bad) for arguments, what does good and bad do, i'm confused with how arguments work honestly. So here couldn't that part of code have been written without them or did it need them to write the code?"

get_guess is doing more than simply getting a guess. It checks a couple of things before returning the guess. In order of the code, here is what it does. First it checks that it has a length of 1. Second it checks that it hasn't already been guessed. It does this by checking if it is in the bad or good lists. If you look at lines 69-79 you see how all guesses get appended to either the good list (if in the secret_word) or the bad list (if not). Finally it makes sure it is a letter. If it doesn't pass any of those steps a message notifying the player is printed. Then, after it has checked all that stuff, it returns the guess. Printing is useful for displaying information, but that is not what you need within the internal logic of the code. When the get_guess() function is used in the play() function (line 67) it isn't useful to display it to the player (who just typed it in); the important thing is to be able to use that returned value in the code that follows.

"then line 71, what in the world is found = True for? I'm very confused here, as well as line 74 where found is now false? Then line 75 where, if found: what does this mean? that whole part of code from line 71 to 75 has me lost."

If you look down to line 75 we see that if found is True, the player wins. So this is our win condition. The found variable is just a way of checking whether the player won. Obviously, we have to create this variable before we check its value. That is what happens at line 71. If the player's guess is in the word, we assign found to True. Then we check whether they really found the word. The code from 72-74 goes through each letter in the secret word and changes found to False if it hasn't been guessed by the player. Imagine the secret word is "hello" and a player has already guessed ('l','o'). Now they guess 'h'. Since this guess is in the secret word we add 'h' to the list of correct guesses (good.append(guess)), set found = True, and then see if all the letters in 'hello' are in good ('l','o','h'). When it checks for 'e' it will not find it in good, so it found becomes False and we we get to line 75 none of the code on 77-77 runs. Now, imagine the next round the player guesses 'e' correctly. This gets appended to good so that when the "for letter in secret_word" block runs it doesn't set found = False. Since found is "still" True when we get to line 75, the players is informed they have won.

1 Answer

thank you joseph r, that helps me a lot! So now my question is for done = true on line 83, so i see that wont run until you guess wrong 7 times, then done is true and runs the if done block, if it remains true it exits the game, but if it becomes false it re runs the play function, so my question is how does that work? like the return play(done=false) how does that make the game run again? Would it work if done was not a argument?