WEBVTT

0
00:00.230 --> 00:00.620
All right.

1
00:00.620 --> 00:05.630
So now that we've reached Step 3, we are moving onwards and upwards.

2
00:05.630 --> 00:11.780
And we're going to add a little bit more functionality to our hangman game so that it functions more,

3
00:11.780 --> 00:13.760
and more like a real game.

4
00:13.760 --> 00:21.170
So in this particular step, we're going to add the ability for the user to continue guessing again, and

5
00:21.170 --> 00:21.920
again.

6
00:21.920 --> 00:29.960
Repeating the hangman checks until they get all the letters in the word.

7
00:29.960 --> 00:37.610
So if you go ahead and right-click on solution.py, and then run that solution, we can see what the end

8
00:37.610 --> 00:40.100
result of this step will look like.

9
00:40.100 --> 00:42.410
So we have the chosen_word,

10
00:42.410 --> 00:45.380
we have the number of characters in that word,

11
00:45.380 --> 00:48.290
and then we are asked to guess a letter.

12
00:48.290 --> 00:56.600
So if I start from the beginning and I guess "c", now what we want to happen is this we're getting another

13
00:56.600 --> 00:58.400
opportunity to guess.

14
00:58.400 --> 01:04.400
And we want this to repeat by writing a while loop in our code.

15
01:04.400 --> 01:06.650
So that's TODO Number 1.

16
01:06.650 --> 01:16.190
Now the next thing we want to do is to update this for loop here, so that if we guess a previous correct

17
01:16.190 --> 01:23.160
letter such as the C in this case, when we guess another letter correctly, we want to keep the previous

18
01:23.160 --> 01:29.880
correct guess and the new letter and add all of that to our variable called display.

19
01:29.970 --> 01:37.440
Now, in contrast, let's take a look what happens at the moment if we use our current state of our

20
01:37.440 --> 01:39.300
code in main.py.

21
01:39.420 --> 01:49.380
So if I guess "a" and all the A's in aardvark gets filled in, the next time that I guess this

22
01:49.380 --> 01:58.500
word, my previous ones will be lost because I'm again checking against the current guess.

23
01:58.500 --> 02:02.460
So think about how you might solve that.

24
02:02.460 --> 02:10.110
And if you get stuck, there's always hints in the Description box for you, but there is quite a bit

25
02:10.110 --> 02:15.600
of thinking and quite a bit of struggling in this particular program that you're writing.

26
02:15.600 --> 02:21.930
But as always, it's really good when you struggle because that means you're learning something.

27
02:21.930 --> 02:27.180
If you're not struggling at all, then this part is probably too easy for you and you need to skip ahead

28
02:27.180 --> 02:28.320
quite a few days.

29
02:28.320 --> 02:35.280
But if you are struggling, then that means you are at the right stage in your programming journey and

30
02:35.280 --> 02:37.950
you will eventually get to where you want to be.

31
02:38.490 --> 02:42.510
So I'm going to go quiet now and let you have time to think.

32
02:42.510 --> 02:49.050
So pause the video, read the Description, try out the solution, run, and see if you can figure out

33
02:49.050 --> 02:51.720
these two TODOs in step three.

34
03:01.980 --> 03:04.290
All right, let's go through the solution.

35
03:04.290 --> 03:12.210
Now, if you haven't attempted to solve this, I recommend to stop, and don't watch the solution just

36
03:12.210 --> 03:12.630
yet.

37
03:12.630 --> 03:18.840
It's only worth watching the solution if you've actually tried at least for ten 20 minutes,

38
03:18.930 --> 03:25.330
because otherwise, the solution is just going to be somebody reading to you a bunch of code and

39
03:25.330 --> 03:27.970
you won't do any learning.

40
03:27.970 --> 03:35.830
And I'm really, really against this idea of spoon-fed education where we're just told what the

41
03:35.830 --> 03:41.650
answer is, because without the thinking phase, we're not truly learning, we're just repeating somebody

42
03:41.650 --> 03:42.670
else's thought.

43
03:42.670 --> 03:45.370
And the most important thing to learn is how to think.

44
03:45.370 --> 03:50.140
That's what a really valuable program of skill is.

45
03:50.290 --> 03:51.910
Okay, so I believe you.

46
03:51.910 --> 03:52.870
You've already tried it.

47
03:52.870 --> 03:55.510
Now let's see if we can go through the solution.

48
03:55.510 --> 04:00.520
So the first step is to use a while loop to get let the user guess again.

49
04:00.520 --> 04:07.750
Because at the moment once they make this one guess, then everything continues and we are at the end

50
04:07.750 --> 04:09.910
of the script.

51
04:09.910 --> 04:12.610
Now, how do we make them guess again?

52
04:12.610 --> 04:14.950
Well, we would need a while loop.

53
04:14.950 --> 04:23.080
So while some condition is True, or while some condition is False, then we want to continue running

54
04:23.080 --> 04:24.490
a block of code.

55
04:24.490 --> 04:30.850
And then when that condition switches, we skip out of the loop and we continue in our code.

56
04:30.850 --> 04:33.490
So what is going to be that condition?

57
04:33.490 --> 04:41.950
Well we can have a condition called, game_over, and we can set that to False.

58
04:42.550 --> 04:45.040
Now, we can check for that condition.

59
04:45.040 --> 04:48.340
We can say while not game over,

60
04:48.340 --> 04:53.050
so while game_over is False, we want the user to make some guesses,

61
04:53.050 --> 04:55.810
we want to update this display,

62
04:55.810 --> 05:02.410
and we also want to do the rest of the code because we need to check through the letter of their guess

63
05:02.410 --> 05:09.610
and see if we can update the display, et cetera, et cetera. So all of the rest of the code actually go into the

64
05:09.610 --> 05:11.650
while loop, and by go in,

65
05:11.650 --> 05:19.000
if you remember, it means that they get indented one step so that they're inside this while heading.

66
05:19.360 --> 05:25.360
Now when do we actually change the while loop so that they can come out of it?

67
05:25.360 --> 05:33.440
Well, if we have these blanks in our display to represent the unguessed letters, if all the unguessed

68
05:33.440 --> 05:40.130
letters are guessed and there are no more underscores, well then, at that point that's when our game

69
05:40.130 --> 05:41.150
is over, right?

70
05:41.150 --> 05:45.710
So let's add an if statement and check if display...

71
05:45.800 --> 05:56.000
And there's a really useful function in Python which is called in, and what we can do with in is we

72
05:56.000 --> 05:59.600
can check to see if something exists in something else.

73
05:59.600 --> 06:10.670
So we can say if this underscore ( _ ) is not in our display variable,

74
06:10.670 --> 06:15.410
so this string does not contain any of these underscores,

75
06:15.410 --> 06:19.400
well at that point, they've guessed everything because there's no more blanks.

76
06:19.400 --> 06:21.050
They've guessed all the letters.

77
06:21.050 --> 06:23.150
Then at this point, we can print.

78
06:23.150 --> 06:24.260
"You win."

79
06:24.620 --> 06:33.050
And that's also the right time point when we can change our game over so that we set it to, True, because

80
06:33.050 --> 06:33.830
they've won,

81
06:33.830 --> 06:35.750
therefore, game is over.

82
06:35.750 --> 06:38.960
Now we're going to define other conditions for game_over later on,

83
06:38.960 --> 06:45.890
but for now, at least, we have one place where our code can exit the while loop, which is really,

84
06:45.890 --> 06:50.690
really important when you're writing while loops, because otherwise you get into this infinite loop

85
06:50.690 --> 06:52.460
situation and that's not good.

86
06:52.460 --> 06:57.920
So let's test our first TODO and see if it does what we expect it to do.

87
06:57.920 --> 07:04.880
So aardvark, let's guess a letter, "a" a gets filled in and then it's asking us, Guess another letter.

88
07:04.880 --> 07:05.240
Right?

89
07:05.240 --> 07:08.370
So let's keep typing because we know what the word is.

90
07:08.370 --> 07:11.640
But now we encounter another issue,

91
07:11.640 --> 07:19.680
because notice how the "r" is being placed into our display variable,

92
07:19.680 --> 07:23.310
and the rest of them are filled in with blanks,

93
07:23.310 --> 07:30.690
but our previous guess, the "a" has disappeared from the display variable, and it's replaced by blanks.

94
07:30.690 --> 07:37.800
So in this particular situation, we'll never get to the state where there are no more underscores in

95
07:37.800 --> 07:45.390
the display, because each time this while loop runs, we're only ever checking one letter, which is

96
07:45.390 --> 07:47.520
the current guest letter.

97
07:47.550 --> 07:54.810
So to fix that, we have to go into TODO Number 2 and change the for loop so that we can keep the

98
07:54.810 --> 07:59.160
previous correct letters in the display string.

99
07:59.160 --> 08:00.870
So how do we do this?

100
08:00.870 --> 08:02.700
There's many ways of doing this,

101
08:02.700 --> 08:07.890
and one way of doing this is simply by creating a list.

102
08:07.920 --> 08:14.700
Now notice I'm going outside the while loop to create this list, because I want this list to accumulate

103
08:14.700 --> 08:19.620
while the loop runs, so that each time it's not being wiped blank.

104
08:19.620 --> 08:30.510
So if I create a new list here, which is called correct_letters and set it to a blank or an empty list

105
08:30.510 --> 08:39.060
like so, if this was placed inside the while loop, then every time the loop repeats, that list is

106
08:39.060 --> 08:45.030
going to be wiped to zero because of this part here in this line.

107
08:45.030 --> 08:48.180
So instead we want it to be outside the while loop.

108
08:48.180 --> 08:56.190
And now what we're going to do is if the letter that we're looping in through the word, is matching the

109
08:56.190 --> 09:03.390
guessed letter by the user, then not only do we add that letter to the display, but we also add that

110
09:03.390 --> 09:06.570
letter to our list of correct letters.

111
09:06.570 --> 09:13.390
So correct letters.append() and we're going to append this current letter that we're looping through.

112
09:13.390 --> 09:15.430
You can also append the guess.

113
09:15.430 --> 09:18.070
But at this point they are exactly equal.

114
09:18.070 --> 09:20.500
So it doesn't really matter which one you choose.

115
09:20.500 --> 09:24.040
It just depends on which you think is more readable.

116
09:24.910 --> 09:30.850
Now if we rerun this code, it doesn't really change much, right?

117
09:30.850 --> 09:34.630
If I change a letter, it's still not doing what I want.

118
09:34.630 --> 09:39.310
So what I have to do also is probably add another check.

119
09:39.310 --> 09:43.450
So I'm going to check another condition using an elif.

120
09:43.450 --> 09:50.800
So if the current guess matches the letter that we're currently looping on, then we add the letter

121
09:50.800 --> 09:55.750
to the display and if it doesn't match, then we add an underscore.

122
09:55.750 --> 09:57.940
But there's another condition we need to check.

123
09:57.940 --> 10:06.250
We need to check, elif, the current letter that we're looping in, is in the list of correct_letters.

124
10:06.250 --> 10:13.090
Because remember, if on a previous run of the while loop, a letter was guessed to be correct, then

125
10:13.090 --> 10:16.000
it gets added to this list of correct letters.

126
10:16.000 --> 10:22.330
And so now whenever we check our letters and we want to fill in the other letters in the display, we

127
10:22.330 --> 10:24.100
need to make an elif.

128
10:24.280 --> 10:26.890
So in this case, what are we going to do?

129
10:26.920 --> 10:30.790
We're going to add the current_letter to the display.

130
10:30.790 --> 10:33.160
So similar to this line here.

131
10:33.160 --> 10:38.680
So now if we rerun this code and we add a letter.

132
10:38.680 --> 10:41.560
So now "b" has been added to the display,

133
10:41.560 --> 10:43.090
let's try "n".

134
10:43.090 --> 10:47.140
So you can see now b is added but n is also added.

135
10:47.140 --> 10:49.840
So the flow goes like this.

136
10:49.840 --> 10:52.690
The four letter in chosen_word,

137
10:52.690 --> 10:56.620
the first one that gets turned into a letter is b.

138
10:56.620 --> 11:00.130
So we're checking is b equal to the guess at this point?

139
11:00.130 --> 11:00.850
Yes.

140
11:00.850 --> 11:05.230
So then we add b to the display and we add blanks to the rest of it.

141
11:05.230 --> 11:09.520
Now then the correct letter is added to this list.

142
11:09.520 --> 11:12.580
So b now lives inside this list.

143
11:12.580 --> 11:18.440
And notice it's outside the while loop so it doesn't get modified when the while loop repeats from the

144
11:18.440 --> 11:20.150
top of the block of code.

145
11:20.150 --> 11:24.080
So the next time this runs, we're guessing a different letter,

146
11:24.080 --> 11:32.000
we're guessing "n". So when n is true, then we are adding n to our display,

147
11:32.000 --> 11:39.380
but when something else is not one of the letters that we're looping in, it's not matching guess, for

148
11:39.380 --> 11:44.270
example b, but it is in fact inside the list of correct letters,

149
11:44.270 --> 11:51.290
then that letter also gets added to the display. And this might be something that you want to try and

150
11:51.290 --> 12:00.170
run in Thonny if it is confusing, but if it all makes sense, then feel free to continue to the next

151
12:00.170 --> 12:02.960
step in the game.