1
00:00:00,070 --> 00:00:00,400
All right.

2
00:00:00,400 --> 00:00:05,400
So how did you get on? Now if you got stuck or you want to see maybe how I did

3
00:00:05,530 --> 00:00:09,160
it, then continue watching and I'll go through the solution with you.

4
00:00:09,820 --> 00:00:13,870
So first things first, head over to the link and fork your own copy.

5
00:00:15,640 --> 00:00:18,730
Once you've done that, we're going to tackle the to-dos one by one

6
00:00:18,730 --> 00:00:20,560
in order. The first one

7
00:00:20,560 --> 00:00:25,560
asks us to randomly choose a word from the word_list and assign it to a variable

8
00:00:27,820 --> 00:00:28,870
called chosen_word.

9
00:00:29,500 --> 00:00:32,890
So in order to randomly get an item out of the list,

10
00:00:33,190 --> 00:00:36,580
we're going to need to import the random module.

11
00:00:37,900 --> 00:00:42,760
And once we've got hold of that, then we can say random.choice

12
00:00:43,030 --> 00:00:45,490
and if we put inside the parentheses

13
00:00:45,790 --> 00:00:48,610
the list that we want to randomly choose from,

14
00:00:48,970 --> 00:00:53,830
then we should be able to get hold of a single item from this list randomly.

15
00:00:54,370 --> 00:00:58,090
And then finally, we're going to assign it to a variable called chosen_word.

16
00:00:58,510 --> 00:01:03,490
So let's call that chosen_word and then set it to equal this result.

17
00:01:04,900 --> 00:01:09,900
So that's to-do 1 completed. The next todo we have to ask the user to guess a

18
00:01:10,060 --> 00:01:15,060
letter and assign their answer to a variable called guess. And finally make that

19
00:01:16,030 --> 00:01:20,650
guess lowercase. In order to ask the user for anything,

20
00:01:20,680 --> 00:01:24,490
we're going to have to create an input. And let's say,

21
00:01:24,550 --> 00:01:26,650
"Guess a letter:"

22
00:01:29,470 --> 00:01:31,690
And then we can assign their answer

23
00:01:31,720 --> 00:01:36,190
which will replace this line of code with a variable called guess.

24
00:01:36,430 --> 00:01:40,210
So we'll just set that equal to guess. And finally,

25
00:01:40,240 --> 00:01:45,240
we're going to make this guess lower case and we do that by writing .lower.

26
00:01:46,840 --> 00:01:51,790
Now step 3 ask us to check if the letter the user guessed,

27
00:01:51,970 --> 00:01:56,970
which is this guess variable, is one of the letters in the chosen_word.

28
00:01:57,550 --> 00:02:02,550
So this randomly chosen word could be any one of these: ardvack, baboon, or camel.

29
00:02:04,210 --> 00:02:08,080
And we have to somehow check if this letter

30
00:02:08,410 --> 00:02:13,410
which is stored inside this variable guess matches any of the letters inside

31
00:02:13,780 --> 00:02:17,890
this list of words. So, as I mentioned as a hint,

32
00:02:18,100 --> 00:02:22,120
you can have a look at this document from Google for education,

33
00:02:22,420 --> 00:02:24,550
which talks about various things in Python.

34
00:02:25,000 --> 00:02:28,690
And the part I wanted to draw your attention to is the for loop

35
00:02:28,720 --> 00:02:33,400
which we covered previously. So we know that we can use the for loop like this

36
00:02:33,700 --> 00:02:38,700
to loop through a particular list and get hold of each of the items in the list,

37
00:02:40,060 --> 00:02:43,150
like this. So in our case,

38
00:02:43,180 --> 00:02:48,180
all we have to do is say for letter in chosen word,

39
00:02:49,000 --> 00:02:54,000
so this is going to go through this chosen word letter by letter

40
00:02:55,390 --> 00:02:57,130
and each time the loop runs,

41
00:02:57,400 --> 00:03:00,460
we'll get hold of one of letters inside the word.

42
00:03:00,850 --> 00:03:05,290
So the first time that letter variable will equal a, the next time

43
00:03:05,320 --> 00:03:08,140
it will be equal to r and so on and so forth.

44
00:03:08,680 --> 00:03:10,900
So now inside our for loop

45
00:03:10,990 --> 00:03:15,990
we can check to see if the letter that we're currently looking at is double

46
00:03:18,370 --> 00:03:23,370
equals to the letter that the user guessed and in this case,

47
00:03:23,530 --> 00:03:25,480
well then that means its a match, right?

48
00:03:25,510 --> 00:03:29,470
So we're going to print either a match or in my case, I'm just going to write,

49
00:03:29,800 --> 00:03:33,850
uh, Right. And else, namely if it doesn't match,

50
00:03:33,910 --> 00:03:37,510
then we're going to print wrong. That's it.

51
00:03:37,570 --> 00:03:40,870
That's all we have to do to complete all three to-dos.

52
00:03:41,380 --> 00:03:43,210
And now if I run this code,

53
00:03:43,330 --> 00:03:47,980
you can see it's going to ask me to guess a letter. So let's put a,

54
00:03:48,640 --> 00:03:51,850
and then it goes through this loop line by line

55
00:03:51,880 --> 00:03:56,740
to check against this letter that I've guessed. So I'm guessing in this case

56
00:03:56,740 --> 00:03:59,230
the chosen word is aardvark.

57
00:03:59,590 --> 00:04:04,420
So when the loop is running through the first letter a and a match,

58
00:04:04,480 --> 00:04:08,740
so it says right. Then the next time it's looking at the letter

59
00:04:08,800 --> 00:04:11,080
r, a and r does not match

60
00:04:11,080 --> 00:04:16,079
so we get wrong and it continues until it finds all the matches and all the ones

61
00:04:16,930 --> 00:04:21,700
that don't match. Now, if you want to check your code against mine,

62
00:04:22,089 --> 00:04:26,890
simply head over to the URL that's listed in the course resources under app

63
00:04:26,890 --> 00:04:29,560
brewery/day-7-Hangman-1-End

64
00:04:30,010 --> 00:04:33,070
and here you'll be able to see all the code that I went through with you just

65
00:04:33,070 --> 00:04:36,430
now. Now, if you had any issues

66
00:04:36,460 --> 00:04:41,260
understanding how the code worked or how to get to the final solution,

67
00:04:41,680 --> 00:04:43,300
and if you're at all confused,

68
00:04:43,390 --> 00:04:48,390
I recommend putting the code through Thonny and just clicking on the debug to go

69
00:04:48,880 --> 00:04:53,290
through each of the steps one by one. So in this line,

70
00:04:53,320 --> 00:04:58,320
we're going to use the random module and get hold of a random item from this

71
00:05:01,360 --> 00:05:02,193
list.

72
00:05:02,260 --> 00:05:07,260
So, in this case, it's picked camel. And now that camel is going to be equal to

73
00:05:08,380 --> 00:05:10,600
the variable chosen_word.

74
00:05:11,260 --> 00:05:15,010
So on the next step we ask the user to guess a letter.

75
00:05:15,640 --> 00:05:17,290
And once we step into that,

76
00:05:17,530 --> 00:05:21,610
it's going to print that out into the debug console.

77
00:05:21,910 --> 00:05:26,910
And if I put a and hit enter, then guess is now equal to a,

78
00:05:30,280 --> 00:05:33,610
and there's our second variable guess, which is equal to a.

79
00:05:34,270 --> 00:05:38,800
Now we fall into our for loop. And if we step into the loop,

80
00:05:39,100 --> 00:05:43,900
then you can see that the chosen_word is going to be replaced with camel and

81
00:05:43,930 --> 00:05:48,930
letter is going to be replaced with each of the letters in the chosen word one

82
00:05:49,060 --> 00:05:52,030
by one. So the first time loop runs, it's going to be c,

83
00:05:52,630 --> 00:05:57,590
and then it checks to see, well, if c is equal to guess,

84
00:05:57,620 --> 00:06:00,890
which is a, does that match? No, it doesn't.

85
00:06:00,890 --> 00:06:05,060
So it jumps to the else part and it prints out wrong.

86
00:06:07,460 --> 00:06:10,340
And then the loop is going to restart from the top

87
00:06:10,400 --> 00:06:13,130
and we're going to go to the next letter in the chosen word,

88
00:06:13,460 --> 00:06:17,480
which is a. Now in this case, the letter is a

89
00:06:17,900 --> 00:06:22,820
and the guest is a and so that should match. And so it should print,

90
00:06:22,940 --> 00:06:23,773
right.

91
00:06:25,160 --> 00:06:29,030
And then it basically steps through all of these again and again

92
00:06:29,090 --> 00:06:33,230
until we get to the end of the word and we get this printed out.

93
00:06:33,800 --> 00:06:38,090
So have a play around with that and the debug console to wrap your head around

94
00:06:38,090 --> 00:06:40,940
it. And once you're confident, you know, what's going on,

95
00:06:41,210 --> 00:06:44,240
then head over to the next lesson and let's complete step 2.

