1
00:00:00,430 --> 00:00:03,969
All right, let's go ahead and run through the solution together. Firstly,

2
00:00:03,969 --> 00:00:07,870
I'm going to fork my own copy of the starting file for step two

3
00:00:08,530 --> 00:00:11,890
and then I'm going to take a look at to-do number one.

4
00:00:12,400 --> 00:00:15,880
So the goal is to create an empty list called display,

5
00:00:16,480 --> 00:00:19,810
and for each letter in the chosen word,

6
00:00:20,050 --> 00:00:22,600
remember that a random word from this list,

7
00:00:23,050 --> 00:00:27,580
we're going to add an underscore to this list called display.

8
00:00:28,270 --> 00:00:30,850
So it tells us that if the word was apple,

9
00:00:30,850 --> 00:00:35,650
then display should contain five underscores representing each letter the

10
00:00:35,650 --> 00:00:36,640
user needs to guess.

11
00:00:37,570 --> 00:00:41,260
Let's go ahead and just click run before doing anything first.

12
00:00:41,770 --> 00:00:46,210
And this print statement will execute and it'll tell us that the solution is

13
00:00:46,240 --> 00:00:49,870
ardvark. This is a seven letter word. So in this case,

14
00:00:49,900 --> 00:00:54,900
we would need to create a list with seven underscores and seven items.

15
00:00:55,630 --> 00:00:58,630
Let's go ahead and create an empty list called display.

16
00:00:59,230 --> 00:01:03,640
An empty list is just a set of square brackets with nothing inside.

17
00:01:04,300 --> 00:01:07,090
Now, for each letter in the chosen word,

18
00:01:07,150 --> 00:01:10,690
we're going to add a underscore to this list.

19
00:01:11,320 --> 00:01:14,230
How can we add something for each letter?

20
00:01:14,770 --> 00:01:19,420
The simplest way is to simply use a for-loop. Now, in this case,

21
00:01:19,450 --> 00:01:20,283
we have a choice.

22
00:01:20,590 --> 00:01:24,850
We can either use the style of for-loop that we used previously down here,

23
00:01:25,270 --> 00:01:28,060
which is a way of looping through a list.

24
00:01:28,390 --> 00:01:31,240
So we could say for letter in chosen_word,

25
00:01:31,600 --> 00:01:35,620
so it's going to run this for as many letters as there are in this word.

26
00:01:36,250 --> 00:01:38,260
And for every single letter,

27
00:01:38,590 --> 00:01:43,590
we're just simply going to take our list, display, and add a item to it.

28
00:01:44,230 --> 00:01:47,230
And that item is going to be the string, underscore.

29
00:01:48,190 --> 00:01:53,190
So now if we go ahead and print out our display and see what we get.

30
00:01:56,080 --> 00:01:57,580
The solution is baboon

31
00:01:57,610 --> 00:02:02,440
which has six letters and display gets printed with one, two, three, four,

32
00:02:02,440 --> 00:02:05,050
five, six underscores inside.

33
00:02:05,680 --> 00:02:10,680
Now notice how here we're not actually using this variable letter that gets

34
00:02:10,750 --> 00:02:13,990
created. We're looping through every letter in the chosen word,

35
00:02:14,020 --> 00:02:17,920
but we're not doing anything with each of the letters. So we can in fact

36
00:02:17,920 --> 00:02:21,610
just replace it with an underscore. Now, alternatively,

37
00:02:21,640 --> 00:02:25,720
we could have created this for loop with a range function as well.

38
00:02:26,230 --> 00:02:31,230
And remember that the range function creates a range of numbers from zero up to

39
00:02:32,290 --> 00:02:36,760
a particular number that you specify. So we could have also,

40
00:02:37,060 --> 00:02:41,680
instead of looping through the chosen word, use range and inside here,

41
00:02:41,680 --> 00:02:45,250
we could have put the length of the chosen word.

42
00:02:46,210 --> 00:02:51,040
So then our range would be between zero and the length of the chosen word.

43
00:02:51,310 --> 00:02:56,080
So it was a baboon then it would be from zero to six, but not including six.

44
00:02:56,080 --> 00:02:58,600
Remember that's how the range operator works.

45
00:02:59,050 --> 00:03:04,050
So it would be zero and then it would be one and so on and so forth until it

46
00:03:04,210 --> 00:03:05,560
gets to five.

47
00:03:06,280 --> 00:03:11,280
And so that would be a total of six loops adding six of these underscores.

48
00:03:11,710 --> 00:03:15,220
So given that I've updated this for loop, if we run it again,

49
00:03:15,430 --> 00:03:17,140
you can see its exactly the same,

50
00:03:17,380 --> 00:03:21,430
it will create as many underscores in the list as there are letters in the

51
00:03:21,430 --> 00:03:25,150
chosen word. So it doesn't actually matter in this case

52
00:03:25,240 --> 00:03:28,780
which version you went for, either this version of the

53
00:03:28,780 --> 00:03:32,200
for loop using the range function or the previous one

54
00:03:32,410 --> 00:03:37,210
which just looped through the list. It's your choice. They both worked the same.

55
00:03:38,050 --> 00:03:41,380
So now let's move on to to-do number two.

56
00:03:41,950 --> 00:03:45,550
Now we have to loop through each position in the chosen word

57
00:03:46,060 --> 00:03:50,350
and if the letter at that position matches the letter the user guessed,

58
00:03:50,650 --> 00:03:55,090
then we're going to reveal that letter in the display at that position.

59
00:03:55,120 --> 00:04:00,120
So basically we're going to replace the underscore with the letter that the user

60
00:04:00,220 --> 00:04:02,500
guessed if they guessed it right.

61
00:04:03,850 --> 00:04:08,850
So now this is probably the hardest part of this whole step because in order to

62
00:04:09,700 --> 00:04:10,420
do this,

63
00:04:10,420 --> 00:04:14,800
we actually have to change this for loop to

64
00:04:14,800 --> 00:04:19,800
one that uses the range function because we need to get hold of the position

65
00:04:20,800 --> 00:04:25,000
when the letter and the guess actually matched. That way

66
00:04:25,030 --> 00:04:30,030
we can tap into the list and change only the item at that particular position.

67
00:04:31,810 --> 00:04:33,970
So let me show you how the code would look.

68
00:04:34,450 --> 00:04:36,790
Instead of saying for letter in chosen_word,

69
00:04:36,820 --> 00:04:41,020
we would say for position in range

70
00:04:42,850 --> 00:04:47,290
and the range is going to be between zero and the length of the chosen_word.

71
00:04:47,740 --> 00:04:50,080
And once we've got that, then the

72
00:04:50,080 --> 00:04:55,080
for-loop is going to run for as many times as there are letters in the word and

73
00:04:55,120 --> 00:04:56,500
for each of those letters

74
00:04:56,530 --> 00:05:00,250
it's going to give us a number, a position to work with.

75
00:05:00,670 --> 00:05:05,530
So the first time it runs it position is going to be equal to zero because we'll

76
00:05:05,530 --> 00:05:09,490
be looking at the first item in the chosen_word,

77
00:05:09,610 --> 00:05:11,170
which in this case would be a.

78
00:05:11,620 --> 00:05:14,620
And then the next time the loop runs position becomes one

79
00:05:14,920 --> 00:05:17,440
and we'll be looking at the second item in the list.

80
00:05:18,610 --> 00:05:21,850
Now we no longer have that letter to work with,

81
00:05:22,030 --> 00:05:24,130
which is why we get an error here.

82
00:05:25,000 --> 00:05:29,440
So how can we get hold of the current letter in the chosen word

83
00:05:29,470 --> 00:05:33,640
given that we know the position? Well,

84
00:05:33,640 --> 00:05:38,640
it would be as simple as saying letter = chosen_word 

85
00:05:40,420 --> 00:05:41,320
[position].

86
00:05:42,250 --> 00:05:47,170
Now we can check to see if the letter is equal to the letter

87
00:05:47,170 --> 00:05:48,130
the user guessed.

88
00:05:48,670 --> 00:05:53,670
Then we can get hold of our display and get the item at the current position and

89
00:05:57,380 --> 00:05:59,900
then set that to equal the letter.

90
00:06:01,700 --> 00:06:04,160
And we can get rid of the else statement entirely.

91
00:06:07,990 --> 00:06:12,460
So now notice that we're using the length of the chosen word in quite a few

92
00:06:12,460 --> 00:06:14,830
places. So if you want to,

93
00:06:14,830 --> 00:06:19,830
you can actually simplify the code by simply creating a new variable called word

94
00:06:19,990 --> 00:06:20,823
length.

95
00:06:23,410 --> 00:06:26,380
And now every time we need to use the word_length,

96
00:06:26,410 --> 00:06:28,990
we can just refer to that variable

97
00:06:31,240 --> 00:06:31,560
Right?

98
00:06:31,560 --> 00:06:34,140
which should make our code a little bit easier to read.

99
00:06:35,480 --> 00:06:36,310
Right?

100
00:06:36,310 --> 00:06:41,020
Finally, all we have to do for step three is just to print our list

101
00:06:41,080 --> 00:06:44,740
which is called display. And then when we run our code,

102
00:06:45,190 --> 00:06:48,730
you should be able to see that if the solution was camel,

103
00:06:49,180 --> 00:06:54,180
initially, our display starts out with five blanks because there's five letters.

104
00:06:55,120 --> 00:06:57,130
And then once we've guessed a letter,

105
00:06:57,550 --> 00:07:02,550
then it will replace that letter at the correct position in that display list.

106
00:07:04,420 --> 00:07:07,720
So a only matches the second letter,

107
00:07:08,050 --> 00:07:11,650
so that second blank gets replaced by this letter.

108
00:07:12,610 --> 00:07:13,210
And again,

109
00:07:13,210 --> 00:07:18,070
I recommend to review the code if it's at all confusing or if you didn't manage

110
00:07:18,070 --> 00:07:23,070
to get it first time through the Thonny IDE just to see it step by step and see

111
00:07:23,410 --> 00:07:27,100
what's actually happening. We've chosen our random word,

112
00:07:27,700 --> 00:07:30,070
which in this case turns out to be camel.

113
00:07:30,610 --> 00:07:34,210
And once we get into our display,

114
00:07:34,240 --> 00:07:36,190
it starts out as an empty list.

115
00:07:36,670 --> 00:07:39,610
But as soon as we calculate the word length,

116
00:07:40,060 --> 00:07:44,380
then we can go ahead and loop through the range of numbers from zero to the

117
00:07:44,380 --> 00:07:49,120
length of the word, which is five. And it will run the code in the

118
00:07:49,120 --> 00:07:49,900
for loop

119
00:07:49,900 --> 00:07:54,640
which is just to add the underscore to this list five times.

120
00:07:54,970 --> 00:07:58,660
And finally, once it's done, it's going to print it out.

121
00:07:59,620 --> 00:08:03,640
So this is what we get, five blanks for the word camel.

122
00:08:04,480 --> 00:08:07,270
Now it asked the user for a guess.

123
00:08:07,390 --> 00:08:12,390
So I'm going to guess that letter a and enters this final for loop.

124
00:08:13,480 --> 00:08:14,950
This is where it's really interesting.

125
00:08:14,950 --> 00:08:17,590
So we're going to step into it to see it in more detail.

126
00:08:18,370 --> 00:08:23,370
So now the range is a range between zero and the word length.

127
00:08:24,070 --> 00:08:25,690
So the word length is five

128
00:08:25,840 --> 00:08:30,010
so that range becomes from zero to five not

129
00:08:30,010 --> 00:08:34,120
including five. So zero, one, two, three, four.

130
00:08:34,539 --> 00:08:37,720
And that is going to make the for loop run five times.

131
00:08:38,470 --> 00:08:43,470
The position starts out with zero. And when position is zero

132
00:08:44,860 --> 00:08:49,860
then it's going to go into this next line, line 18, and put zero here as the

133
00:08:50,860 --> 00:08:54,400
position. And then we go into the chosen word,

134
00:08:54,430 --> 00:08:58,260
which is camel and pick out the item at position zero.

135
00:08:58,980 --> 00:09:03,980
And so this letter becomes equal to the letter c because that's the letter at

136
00:09:04,950 --> 00:09:09,180
position zero of the word. So now in our if statement,

137
00:09:09,300 --> 00:09:13,560
we check to see if the letter, which is c, is equal to the guess,

138
00:09:13,590 --> 00:09:17,280
which is a. Well in this case, c does not equal a

139
00:09:17,790 --> 00:09:22,020
so we go back to the beginning of the for loop and we go to the next number in

140
00:09:22,020 --> 00:09:23,970
the range, which is now one.

141
00:09:24,480 --> 00:09:28,410
And we're going to repeat this again and again. But in this case,

142
00:09:28,470 --> 00:09:33,470
notice that the letter at position one of camel is equal to a.

143
00:09:37,110 --> 00:09:41,670
So now letter is equal to a and guess is equal to a.

144
00:09:41,910 --> 00:09:44,880
So if those two are equal, then this

145
00:09:44,910 --> 00:09:49,910
if statement is true and we end up going inside it to carry out this

146
00:09:50,520 --> 00:09:53,790
functionality. Our letter is equal to a,

147
00:09:54,540 --> 00:09:58,800
and we're going to look at our list, which is called display, this one,

148
00:09:59,430 --> 00:10:03,990
and we're going to replace that second character with the letter that was

149
00:10:03,990 --> 00:10:07,200
matching. And it's going to do this again and again

150
00:10:07,290 --> 00:10:11,970
until we've looped through five times through all of the letters in the chosen

151
00:10:11,970 --> 00:10:12,720
word

152
00:10:12,720 --> 00:10:17,720
and finally we print the final version of display with the correct letters

153
00:10:18,900 --> 00:10:22,500
replaced. Have a play around with this

154
00:10:22,530 --> 00:10:27,420
if you're at all confused and it might be that you need to look up some stuff on

155
00:10:27,450 --> 00:10:28,470
Stack Overflow,

156
00:10:28,710 --> 00:10:33,300
or you might need to review some of the lessons that we've done before on things

157
00:10:33,300 --> 00:10:37,170
like range functions and for loops, just so that you're really,

158
00:10:37,170 --> 00:10:38,370
really certain that you know

159
00:10:38,370 --> 00:10:42,570
what's going on and only then should you continue to the next step.

160
00:10:43,230 --> 00:10:46,560
Take a moment with this and I'll see you on the next lesson.

