All right.
So how did you get on? Now if you got stuck or you want to see maybe how I did
it, then continue watching and I'll go through the solution with you.
So first things first, head over to the link and fork your own copy.
Once you've done that, we're going to tackle the to-dos one by one
in order. The first one
asks us to randomly choose a word from the word_list and assign it to a variable
called chosen_word.
So in order to randomly get an item out of the list,
we're going to need to import the random module.
And once we've got hold of that, then we can say random.choice
and if we put inside the parentheses
the list that we want to randomly choose from,
then we should be able to get hold of a single item from this list randomly.
And then finally, we're going to assign it to a variable called chosen_word.
So let's call that chosen_word and then set it to equal this result.
So that's to-do 1 completed. The next todo we have to ask the user to guess a
letter and assign their answer to a variable called guess. And finally make that
guess lowercase. In order to ask the user for anything,
we're going to have to create an input. And let's say,
"Guess a letter:"
And then we can assign their answer
which will replace this line of code with a variable called guess.
So we'll just set that equal to guess. And finally,
we're going to make this guess lower case and we do that by writing .lower.
Now step 3 ask us to check if the letter the user guessed,
which is this guess variable, is one of the letters in the chosen_word.
So this randomly chosen word could be any one of these: ardvack, baboon, or camel.
And we have to somehow check if this letter
which is stored inside this variable guess matches any of the letters inside
this list of words. So, as I mentioned as a hint,
you can have a look at this document from Google for education,
which talks about various things in Python.
And the part I wanted to draw your attention to is the for loop
which we covered previously. So we know that we can use the for loop like this
to loop through a particular list and get hold of each of the items in the list,
like this. So in our case,
all we have to do is say for letter in chosen word,
so this is going to go through this chosen word letter by letter
and each time the loop runs,
we'll get hold of one of letters inside the word.
So the first time that letter variable will equal a, the next time
it will be equal to r and so on and so forth.
So now inside our for loop
we can check to see if the letter that we're currently looking at is double
equals to the letter that the user guessed and in this case,
well then that means its a match, right?
So we're going to print either a match or in my case, I'm just going to write,
uh, Right. And else, namely if it doesn't match,
then we're going to print wrong. That's it.
That's all we have to do to complete all three to-dos.
And now if I run this code,
you can see it's going to ask me to guess a letter. So let's put a,
and then it goes through this loop line by line
to check against this letter that I've guessed. So I'm guessing in this case
the chosen word is aardvark.
So when the loop is running through the first letter a and a match,
so it says right. Then the next time it's looking at the letter
r, a and r does not match
so we get wrong and it continues until it finds all the matches and all the ones
that don't match. Now, if you want to check your code against mine,
simply head over to the URL that's listed in the course resources under app
brewery/day-7-Hangman-1-End
and here you'll be able to see all the code that I went through with you just
now. Now, if you had any issues
understanding how the code worked or how to get to the final solution,
and if you're at all confused,
I recommend putting the code through Thonny and just clicking on the debug to go
through each of the steps one by one. So in this line,
we're going to use the random module and get hold of a random item from this
list.
So, in this case, it's picked camel. And now that camel is going to be equal to
the variable chosen_word.
So on the next step we ask the user to guess a letter.
And once we step into that,
it's going to print that out into the debug console.
And if I put a and hit enter, then guess is now equal to a,
and there's our second variable guess, which is equal to a.
Now we fall into our for loop. And if we step into the loop,
then you can see that the chosen_word is going to be replaced with camel and
letter is going to be replaced with each of the letters in the chosen word one
by one. So the first time loop runs, it's going to be c,
and then it checks to see, well, if c is equal to guess,
which is a, does that match? No, it doesn't.
So it jumps to the else part and it prints out wrong.
And then the loop is going to restart from the top
and we're going to go to the next letter in the chosen word,
which is a. Now in this case, the letter is a
and the guest is a and so that should match. And so it should print,
right.
And then it basically steps through all of these again and again
until we get to the end of the word and we get this printed out.
So have a play around with that and the debug console to wrap your head around
it. And once you're confident, you know, what's going on,
then head over to the next lesson and let's complete step 2.