WEBVTT

0
00:00.200 --> 00:02.900
So now we're inside Step 4.

1
00:02.900 --> 00:08.090
So we're almost there at the end now closing in on the finish line.

2
00:08.090 --> 00:11.810
So there are three TODOs in this step,

3
00:11.810 --> 00:20.330
and you'll notice that when you look at the starting file I've got the different stages of hangman as

4
00:20.330 --> 00:23.450
ASCII art for us to be able to use.

5
00:23.450 --> 00:27.440
And they go from 6,

6
00:27.440 --> 00:31.050
so which is the start of the hangman

7
00:31.050 --> 00:37.710
with no man, all the way down to 0, where the art is complete,

8
00:37.710 --> 00:41.880
and that's when the user would be in Game Over status.

9
00:41.880 --> 00:45.990
And we're going to combine that, among other things, into our game.

10
00:46.440 --> 00:50.730
So let's first take a look at a run of the solution.

11
00:50.730 --> 00:54.240
So this is what we're aiming to achieve by the end of this step.

12
00:54.240 --> 00:57.070
So we want to be able to guess a letter,

13
00:57.070 --> 01:05.080
and you'll notice that when you guess a correct letter, then the hangman art does not progress.

14
01:05.080 --> 01:11.230
So if the word is baboon I guess "b", then there is firstly art with no man.

15
01:11.230 --> 01:15.610
And then if I guess another correct letter, you'll see the art doesn't change,

16
01:15.610 --> 01:17.500
it's still just the rack.

17
01:17.500 --> 01:21.110
Now if I guess an incorrect letter, let's try F,

18
01:21.110 --> 01:25.910
you'll see that the hangman picture starts appearing.

19
01:25.910 --> 01:34.670
And if I keep guessing incorrect letters, you'll see that the person is slowly being drawn until we

20
01:34.670 --> 01:44.090
get to the very, very end where we end up with the complete image, which is image [0] in our list

21
01:44.090 --> 01:45.350
called stages.

22
01:45.350 --> 01:49.420
And at this point we get the message, "You lose."

23
01:49.420 --> 01:54.070
So that's all that we're trying to achieve in this particular step.

24
01:54.070 --> 02:01.900
And if you take a look in the TODOs, either in the description, now that this is updated, or

25
02:01.900 --> 02:08.710
in the main.py, you should be able to see the individual steps which you have to go through.

26
02:08.860 --> 02:15.860
So TODO Number 1, is to create a variable called lives to keep track of how many lives the user has

27
02:15.860 --> 02:16.370
left.

28
02:16.370 --> 02:25.130
They start out with 6, and once that reaches 0, then the game should stop and it should print,

29
02:25.130 --> 02:26.120
"You lose."

30
02:26.150 --> 02:28.670
So how does it go down to 0?

31
02:28.670 --> 02:35.480
Well, every time the user guesses a letter that is not in the chosen word, then we have to reduce

32
02:35.490 --> 02:42.390
their "lives" by one until eventually at some point it'll reach zero and they lose.

33
02:42.420 --> 02:49.140
Now, finally, we're going to incorporate the ASCII art from this list, which is called stages that

34
02:49.140 --> 02:51.540
I've added in the starting file for you.

35
02:51.540 --> 02:58.590
And you're going to print it out so that it corresponds to the current number of lives the user has

36
02:58.590 --> 02:59.370
remaining.

37
02:59.370 --> 03:08.770
And you might want to print out the stages using different indices or different positions to

38
03:08.770 --> 03:13.870
pick out different pictures and just get a feel for how they're ordered.

39
03:14.560 --> 03:16.510
So that's all there is to it.

40
03:16.510 --> 03:23.680
And if you're ready, then go ahead and pause the video now and attempt Step 4.

41
03:37.220 --> 03:37.760
All right.

42
03:37.760 --> 03:40.580
So let's start from TODO Number 1.

43
03:40.580 --> 03:44.300
This is probably the easiest TODO out of this whole project,

44
03:44.300 --> 03:48.620
create a variable called lives and set it equal to 6.

45
03:48.620 --> 03:50.690
That's it, TODO Number 1 done.

46
03:50.820 --> 03:51.900
Next,

47
03:51.900 --> 04:00.180
if the guess, so the current letter that the user has guessed based on their input is not a letter in

48
04:00.180 --> 04:06.600
the chosen_word, so it doesn't exist in the chosen_word, then we need to reduce lives by 1.

49
04:06.720 --> 04:14.310
Now you might remember that we have this if, elif, else statement here that checks to see if the guess

50
04:14.340 --> 04:18.350
is equal to the letter in the chosen_word.

51
04:18.350 --> 04:24.890
So by looping through each of the letters and one of them matches the guess, then this section does

52
04:24.890 --> 04:31.490
correspond to when the letter is in the chosen_word, but it does not correspond to when the letter

53
04:31.490 --> 04:33.530
is not in the chosen_word.

54
04:33.530 --> 04:38.690
So the situation where the user has guessed the letter that does not exist in the word.

55
04:38.690 --> 04:45.180
So rather than targeting the elif and else, or messing with this if statement, we can actually simply

56
04:45.180 --> 04:52.080
just write a completely separate statement so that we keep our logic separate and under different banners,

57
04:52.080 --> 04:54.930
and it will probably be easier to debug as well.

58
04:54.930 --> 04:59.490
So we can use the "not in" keyword.

59
04:59.490 --> 05:07.330
So we can say if the guess is not in the chosen_word,

60
05:07.510 --> 05:14.740
then under this condition, do we reduce the number of lives -= 1.

61
05:14.830 --> 05:24.940
And then if at this point, after we've subtracted 1 from lives, if lives is now equal to 0 at

62
05:24.940 --> 05:27.310
this point, well then that's game over, right?

63
05:27.310 --> 05:30.010
So we set game_over to True.

64
05:30.010 --> 05:33.710
And that means we break out of our while loop.

65
05:33.710 --> 05:40.160
And in addition, we're also going to tell the user that this has happened by telling them, "You lose."

66
05:40.250 --> 05:42.800
So that's TODO Number 2 tackled.

67
05:42.800 --> 05:50.090
Now the final TODO is simply just to print the ASCII art from this list corresponding to the current

68
05:50.120 --> 05:51.320
number of lives.

69
05:51.320 --> 05:59.730
Now, very helpfully, I've ordered the art in this list so that they correspond to the number of lives,

70
05:59.730 --> 06:02.550
so it starts out at 0,

71
06:02.550 --> 06:08.580
of course. This is what the picture should display when the user has zero lives.

72
06:08.580 --> 06:16.440
And also it goes all the way up to six, so that when the user has 6 lives, full lives, then you don't

73
06:16.440 --> 06:17.970
see the man at all.

74
06:17.970 --> 06:23.470
So that means it's as simple as simply getting the right indentation,

75
06:23.470 --> 06:30.430
so being inside our while loop, because we want to print these ASCII art each time the user makes a

76
06:30.430 --> 06:31.180
guess.

77
06:31.180 --> 06:36.880
And then we simply use the print statement to tap into that list, and then use the square brackets

78
06:36.880 --> 06:42.070
to choose the correct index or the correct position for the art we want to display.

79
06:42.070 --> 06:47.020
And as I just mentioned, it's matched to our lives situation.

80
06:47.040 --> 06:51.180
So that's all that we need to do in order to complete this, to do.

81
06:51.180 --> 06:58.500
And now if you run this code, you'll see that as long as I keep getting the right letter, my art remains

82
06:58.500 --> 07:04.830
on this particular picture, which is corresponding to lives equals six.

83
07:04.830 --> 07:14.020
And then as I make incorrect guesses, then my hangman picture starts progressing all the way until

84
07:14.020 --> 07:15.820
I end up with no lives,

85
07:15.820 --> 07:19.150
which is the last picture here.

86
07:19.150 --> 07:23.770
Lives equals six, and I get the, "You lose" printed out.

87
07:24.430 --> 07:26.950
So did you manage to solve these steps?

88
07:26.950 --> 07:32.440
I would say this particular step is probably a little bit easier than some of the previous ones, but

89
07:32.440 --> 07:40.310
as you've noticed, the difficulty can vary in this project and I just want to guide you from step to

90
07:40.310 --> 07:47.840
step, making sure that each to do and each of the step codes actually make sense.

91
07:47.840 --> 07:53.150
So if there's something you realize about previous steps that doesn't make sense, go back and rewatch

92
07:53.150 --> 07:59.690
it and just make sure that all the code that you see at this stage makes perfect sense to you, and

93
07:59.690 --> 08:02.210
that you would be able to write it from scratch.