1
00:00:00,070 --> 00:00:04,120
All right. So make sure you've given it a good go before you come over here,

2
00:00:04,330 --> 00:00:09,160
because I'm about to reveal the solution. In terms of step 4, here

3
00:00:09,160 --> 00:00:13,660
I've got a forked copy of the code. And if I scroll down to

4
00:00:13,660 --> 00:00:16,180
to-do number one, this is pretty simple.

5
00:00:16,360 --> 00:00:19,300
It just tells us to create a new variable called lives

6
00:00:19,660 --> 00:00:23,350
and this variable is going to be the one that's going to keep track of the

7
00:00:23,350 --> 00:00:27,040
number of lives the user has left. And to start off,

8
00:00:27,070 --> 00:00:32,049
it's going to be equal to six. Now we move on to to-do number two.

9
00:00:32,380 --> 00:00:36,910
And it tells us that if the guess is not a letter in the chosen word,

10
00:00:37,150 --> 00:00:40,210
then we have to reduce the number of lives by one.

11
00:00:40,690 --> 00:00:45,340
Now it might be really tempting to put this as an else statement here,

12
00:00:45,820 --> 00:00:48,250
because if the letter is equal to guess,

13
00:00:48,260 --> 00:00:52,600
then well obviously it matches. Well then else, that means it doesn't match.

14
00:00:53,020 --> 00:00:56,500
But check out what happens here if I just print a simple statement.

15
00:00:58,090 --> 00:01:01,870
So now if I hit run and we take a look on the right side,

16
00:01:02,140 --> 00:01:04,209
you can see that if I guess a,

17
00:01:04,569 --> 00:01:09,190
then you can see that we get 'no match' printed five times,

18
00:01:09,640 --> 00:01:14,640
and that should show you that this is actually not where we want to write the

19
00:01:14,650 --> 00:01:16,300
code for to-do number two,

20
00:01:16,330 --> 00:01:21,330
because remember to-do number two says if the guessed letter is not a letter in

21
00:01:21,520 --> 00:01:26,260
the chosen word, then we should reduce the lives by one. But actually in fact,

22
00:01:26,260 --> 00:01:31,260
what's happening here is it's looping through the entire word length,

23
00:01:31,930 --> 00:01:35,260
and for each of the letters in that chosen word,

24
00:01:35,680 --> 00:01:37,750
if the letter doesn't match the guess,

25
00:01:37,810 --> 00:01:39,580
then it's going to trigger this else statement.

26
00:01:40,780 --> 00:01:43,930
This is not what we want because in this case

27
00:01:43,990 --> 00:01:46,900
a does in fact match a letter in the word baboon,

28
00:01:47,140 --> 00:01:49,690
but we're getting no match being called five times.

29
00:01:50,260 --> 00:01:53,680
So instead, we wanna write it outside of the for loop,

30
00:01:53,710 --> 00:01:55,990
so at the same indentation level,

31
00:01:56,380 --> 00:02:01,380
and the hint is that the to-do is actually at the same level as where your

32
00:02:01,480 --> 00:02:04,150
answer code needs to go. So here,

33
00:02:04,150 --> 00:02:09,150
we're going to write an if statement and we're going to check if the guess is

34
00:02:09,160 --> 00:02:11,560
not in the chosen word.

35
00:02:12,220 --> 00:02:14,560
And basically this does a separate check.

36
00:02:14,590 --> 00:02:17,320
It goes through this entire word and checks

37
00:02:17,350 --> 00:02:20,710
if the guessed letter exists in there at all.

38
00:02:20,950 --> 00:02:22,690
And if this is true,

39
00:02:22,990 --> 00:02:27,850
then we're going to reduce the number of lives by one, so -= 1.

40
00:02:29,110 --> 00:02:30,430
Now, if at this stage

41
00:02:30,460 --> 00:02:35,460
after we've taken a life away and the number of lives goes down to zero, well

42
00:02:36,280 --> 00:02:39,760
then the game should stop and it should print "You lose."

43
00:02:40,210 --> 00:02:44,470
So let's add another if statement in here that checks whether if the number of

44
00:02:44,470 --> 00:02:48,160
lives is equals to, double equal to, zero.

45
00:02:48,340 --> 00:02:53,140
And if this is the case, then we should exit the game. And remember to do that

46
00:02:53,140 --> 00:02:54,730
we need to exit the while loop.

47
00:02:55,180 --> 00:02:58,540
And the while loop is dependent on this variable end_of_game

48
00:02:58,900 --> 00:03:03,430
which starts out being false. And when we want to exit the game,

49
00:03:03,430 --> 00:03:06,850
when it is in fact the end of the game, we switch it to true. Like,

50
00:03:06,850 --> 00:03:10,660
for example, in this case, when the user has won. So here,

51
00:03:10,720 --> 00:03:15,280
when the lives is equal to zero, we can say, end_of_game = True,

52
00:03:16,270 --> 00:03:19,090
and we're going to print "You lose."

53
00:03:19,990 --> 00:03:23,530
So that's all that's needed to tackle to-do number two.

54
00:03:24,490 --> 00:03:26,260
So now let's run our code again.

55
00:03:27,910 --> 00:03:32,500
And you can see that if I keep guessing the wrong letter six times,

56
00:03:34,750 --> 00:03:37,990
then eventually I exit out of my while loop.

57
00:03:38,020 --> 00:03:40,030
You can see my prompt showing up again.

58
00:03:40,420 --> 00:03:44,830
And this means that we've now come out of the game and it's told us that we've

59
00:03:44,830 --> 00:03:45,663
lost.

60
00:03:46,150 --> 00:03:50,020
So now all we have to do is tackle to-do number three.

61
00:03:50,470 --> 00:03:54,940
We have to print the ASCII art from the list called stages

62
00:03:55,270 --> 00:03:59,590
that corresponds to the current number of lives the user has remaining. Now,

63
00:03:59,620 --> 00:04:00,700
luckily for you,

64
00:04:00,730 --> 00:04:05,730
I've arranged each of these pictures in descending order of lives.

65
00:04:06,190 --> 00:04:08,830
So when you have a full six lives,

66
00:04:09,250 --> 00:04:12,700
the image at index six is the starting image.

67
00:04:13,090 --> 00:04:14,770
And when you have no lives,

68
00:04:14,800 --> 00:04:19,800
so the image at index zero is the one where the hangman is fully drawn.

69
00:04:22,120 --> 00:04:26,710
So all we have to do down here is to simply just the print

70
00:04:26,980 --> 00:04:28,300
the stages,

71
00:04:28,900 --> 00:04:33,580
and corresponding to the current number of lives.

72
00:04:33,610 --> 00:04:35,830
So we put lives inside the square brackets.

73
00:04:36,340 --> 00:04:40,180
And now if we run the code again and we keep guessing,

74
00:04:41,920 --> 00:04:46,920
you can see that we're going through our hangman picture until we are fully

75
00:04:47,110 --> 00:04:49,840
drawn and we lose and it's the end of the game.

76
00:04:50,770 --> 00:04:53,170
Have a quick review of the solution

77
00:04:53,620 --> 00:04:55,930
and if you want to see this code that I've written,

78
00:04:55,990 --> 00:04:58,960
then as always, head over to the course resources,

79
00:04:59,200 --> 00:05:02,950
and you'll find a link to Day-7-Hangman-4-End

80
00:05:03,280 --> 00:05:07,510
and you'll be able to see all the code that I've written in there, including,

81
00:05:07,570 --> 00:05:11,500
um, helpful things like a print statement that you can activate,

82
00:05:11,920 --> 00:05:13,840
which just shows you the current position,

83
00:05:13,840 --> 00:05:16,450
the current letter and the letter that's being guessed.

84
00:05:16,480 --> 00:05:18,940
So you can see as the loop is going on

85
00:05:19,210 --> 00:05:24,190
what's actually happening and it'll make it easier for you to debug any problems

86
00:05:24,190 --> 00:05:27,760
in your code. So if you had any problems in the code that you wrote,

87
00:05:28,060 --> 00:05:32,140
this is a really useful line to be able to help you debug what's gone wrong.

88
00:05:32,890 --> 00:05:35,050
Hopefully you manage to do thi okay,

89
00:05:35,200 --> 00:05:39,100
and once you've had a quick review and you're happy with everything that's been

90
00:05:39,100 --> 00:05:40,570
covered in this lesson,

91
00:05:40,840 --> 00:05:44,650
then head over to the next lesson and we're going to complete the final step,

92
00:05:44,650 --> 00:05:47,020
step five of our hangman project.

