WEBVTT

0
00:00.140 --> 00:08.030
So now it's time to move on to Step 2 in the five steps to create our hangman game.

1
00:08.090 --> 00:15.620
Now in Step 2, I recommend reading through the Description for a more detailed breakdown of each

2
00:15.620 --> 00:22.670
TODO, because I don't want to clutter the code file with a bunch of text, but also I need to be able

3
00:22.670 --> 00:28.640
to put the TODOs in so that I can tell you where you need to write each of the blocks of code.

4
00:28.730 --> 00:33.980
So take a look through the TODOs and any hints if necessary.

5
00:34.010 --> 00:39.890
Ideally, if you can create the code for the solution without looking at the hints, that would be great,

6
00:39.890 --> 00:42.560
but the hints are there for a purpose.

7
00:42.560 --> 00:45.350
So if you get stuck, check them out.

8
00:45.440 --> 00:50.540
Now, as always, we're going to start by running the solution code without viewing the solution.

9
00:50.540 --> 00:57.080
And you can see what we're trying to achieve in this step is we've got the chosen_word printed.

10
00:57.770 --> 01:03.890
We want to print out a set of blanks, because when we actually have the final game, we're not going

11
01:03.890 --> 01:05.900
to show the user the solution.

12
01:05.900 --> 01:09.980
Instead, we're going to show them, "This is how many letters you need to guess."

13
01:09.980 --> 01:19.010
And we're showing it to them in these blanks, which are basically created with these underscore symbols.

14
01:19.450 --> 01:24.940
Now the next step is the user will go ahead and guess a letter.

15
01:24.940 --> 01:33.220
And what we want to do in TODO Number 2 is to create a variable called display that is going to put

16
01:33.220 --> 01:37.420
the guessed letter into the right position.

17
01:37.420 --> 01:38.680
So you can see an aardvark.

18
01:38.680 --> 01:43.900
We've got two A's at the beginning and then an A at this position as well.

19
01:43.900 --> 01:51.250
So we're creating a string where the user-guessed letter is put into the right positions of the word,

20
01:51.250 --> 02:00.190
but the unguessed letters are represented by underscores, which will represent a blank for the user

21
02:00.190 --> 02:01.870
to continue guessing.

22
02:01.870 --> 02:07.510
So as we're on Step 2, it's getting harder, but hopefully, you'll be able to figure out how to do

23
02:07.510 --> 02:14.530
this by looking at the TODOs, the hints, and also using your knowledge of Python.

24
02:14.530 --> 02:19.450
So I'm going to let you pause the video now and try to tackle this challenge.

25
02:36.520 --> 02:39.820
Okay, so the first thing we need to do is TODO Number 1.

26
02:39.820 --> 02:45.370
And we're going to write our code right here underneath it we're going to create an empty string called,

27
02:45.370 --> 02:46.180
"placeholder".

28
02:46.180 --> 02:47.860
So that's quite easy.

29
02:47.860 --> 02:53.590
And we're going to set it to equal an empty string with the double quotes with nothing in between them.

30
02:53.620 --> 03:03.190
Next we need to create a for loop so that for each letter in the chosen_word, we're going to add a

31
03:03.190 --> 03:06.820
underscore symbol to this placeholder.

32
03:06.820 --> 03:12.640
So for example, if the chosen word was apple then the placeholder will just be five underscores.

33
03:12.640 --> 03:14.140
So how can we do that.

34
03:14.140 --> 03:19.540
Well we learned about the for in range() function before.

35
03:19.540 --> 03:25.510
So we could for example say for and at this point you can name this variable whatever you like, but

36
03:25.510 --> 03:30.570
I'm going to call it, "position," for each of the positions in the range...

37
03:31.140 --> 03:33.030
Now what is the range that we want.

38
03:33.060 --> 03:39.000
Well, if the word was apple then we would want that range to be the number 6, because we want this

39
03:39.000 --> 03:41.190
loop to run five times.

40
03:41.190 --> 03:46.320
And we know that range goes from the starting point to the end point.

41
03:46.320 --> 03:52.380
So if we wanted to write (1, 6), then go ahead and print,

42
03:52.620 --> 03:58.710
"This loop has run". And I go ahead and run this current file.

43
03:58.710 --> 04:04.680
You'll see it prints us out one, two, three, four, five times,

44
04:04.680 --> 04:12.360
which means that instead of printing, "This loop has run", I could assign the placeholder a new value.

45
04:12.360 --> 04:17.160
So the += will add a new letter to that string.

46
04:17.160 --> 04:24.120
And I could add the underscore so that each time the loop runs we add an underscore to this string placeholder.

47
04:24.120 --> 04:26.610
So let's go ahead and print(placeholder).

48
04:26.610 --> 04:29.370
And let's rerun this current file.

49
04:29.370 --> 04:34.020
And you can see here we've got our string placeholder being printed out,

50
04:34.020 --> 04:42.870
and it's composed of five underscores because our range ran five times between one up to but not including

51
04:42.870 --> 04:43.590
six.

52
04:43.740 --> 04:53.070
So if we want the range to instead run to the number of characters in our chosen_word, then all we

53
04:53.070 --> 05:03.960
need to do is to create a word_length variable and we can set it to equal the length of the chosen_word.

54
05:04.230 --> 05:13.410
And now if we put that in here as the range and go ahead and rerun this, then you can see that we're

55
05:13.410 --> 05:21.450
getting the same number of times that loop runs as the number of characters in the word.

56
05:21.480 --> 05:28.350
So now we're basically creating this placeholder so that the user can look at this instead of the actual

57
05:28.350 --> 05:31.110
answer and try to guess the letter.

58
05:31.800 --> 05:38.370
Now, if this was at all confusing, do go back and revise the lesson where we learned about the range()

59
05:38.370 --> 05:45.360
function in the context of for loops in Python, it hopefully will make a bit more sense, and then

60
05:45.360 --> 05:47.700
you can come back here and fix your code.

61
05:47.970 --> 05:56.160
Now the next TODO is to create a variable called display that puts the guessed letter into the right

62
05:56.160 --> 06:00.680
positions and a blank in the rest of the string.

63
06:00.680 --> 06:09.230
So for example, in the Description here, you can see that if the word was Apple and the user

64
06:09.230 --> 06:16.100
guessed "p", then this display should put the "p" letters in the right positions, and then the rest of

65
06:16.100 --> 06:18.140
the positions have a blank.

66
06:18.140 --> 06:24.290
So we're going to reuse or rather extend this for loop that we created previously.

67
06:24.290 --> 06:28.340
So we know that we can loop through each of the letters in the chosen_word,

68
06:28.340 --> 06:33.530
and if the letter matches the guessed letter then we'll print, "Right",

69
06:33.530 --> 06:35.090
otherwise we'll print wrong.

70
06:35.090 --> 06:41.510
But instead of printing "Right" and "Wrong", what we want to do instead is to create a new variable called

71
06:41.510 --> 06:45.200
display and set this to equal an empty string.

72
06:45.740 --> 06:54.590
Now if in our loop, we check the current letter and it's equal to what the user guessed in their input,

73
06:54.590 --> 07:02.030
then we're going to take this display and we're going to use the += to add the current letter

74
07:02.030 --> 07:05.960
that we're looping through to this display.

75
07:05.990 --> 07:15.050
And if the letter is not a match then we're going to take the display and we're going to give it an

76
07:15.050 --> 07:16.910
underscore instead.

77
07:16.940 --> 07:26.210
So now if I go ahead and finally print out the final value of display at the end of the running of this

78
07:26.210 --> 07:29.450
loop, then we can see it in action.

79
07:29.450 --> 07:33.290
So the current word that's being chosen is aardvark.

80
07:33.320 --> 07:39.020
This is the placeholder being printed out with the number of letters in that word.

81
07:39.020 --> 07:41.360
And then we're asked to guess a letter.

82
07:41.360 --> 07:43.250
So I'm going to guess A.

83
07:43.250 --> 07:48.920
And then you can see that for each time this loop runs, we'll loop through the first letter in the chosen_word

84
07:48.920 --> 07:49.580
,

85
07:49.610 --> 07:53.270
A, is it equal to the guessed letter?

86
07:53.300 --> 07:54.500
Well it is.

87
07:54.500 --> 07:59.000
Well, in that case, we're going to add that letter to the display.

88
07:59.000 --> 08:01.280
And that's how we have this first a.

89
08:01.360 --> 08:09.640
Now when we loop to this R, so this letter is equal to R, "Is the letter equal to guess? No."

90
08:09.640 --> 08:11.500
So it's going to trigger the else.

91
08:11.500 --> 08:17.590
And in this case it just adds an underscore to that string display instead.

92
08:17.710 --> 08:24.970
And by the end of this entire process we end up with the display showing the letters which were correctly

93
08:24.970 --> 08:29.020
guessed, and blanks for the letters which were not.

94
08:29.650 --> 08:35.890
So that's all there is to it, to the two TODOs in Step 2.

95
08:36.160 --> 08:44.590
We're now creating the basis of our game that we're going to build on in future steps. So hopefully

96
08:44.590 --> 08:45.340
that makes sense,

97
08:45.340 --> 08:46.060
if it didn't,

98
08:46.060 --> 08:50.980
as always, go and fix your code and make sure you understand what's going on before you proceed.

99
08:50.980 --> 08:53.170
Otherwise I'll see you in the next lesson.