WEBVTT

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

1
00:00.620 --> 00:02.870
We are very much now in the home stretch.

2
00:02.870 --> 00:07.190
We are on the last step, Step Number 5 of five.

3
00:07.190 --> 00:11.090
And we're going to complete this game in this lesson.

4
00:11.090 --> 00:18.440
So when you open up the starting file in the main.py, you will notice that there are a bunch of errors.

5
00:18.440 --> 00:20.750
And this is intentional.

6
00:20.750 --> 00:27.080
Now what's happened is I have deleted the list called stages.

7
00:27.080 --> 00:36.680
I have deleted the list called word_list, and instead, I want you to get them from these files hangman_art,

8
00:36.710 --> 00:42.650
and hangman_words just so that we can practice using other modules.

9
00:42.650 --> 00:46.010
And this is something we learned in previous lessons as well.

10
00:46.010 --> 00:53.120
So if you forgot, then take a look at Python modules when we introduced the Random Module.

11
00:53.660 --> 01:02.240
So that tackles TODO Number 1, TODO Number 2, and TODO Number 3, which is to basically

12
01:02.240 --> 01:08.870
use what you've learned about importing modules in order to bring in those things that we need for

13
01:08.870 --> 01:10.730
our code to function correctly.

14
01:10.880 --> 01:19.490
Now the next few TODOs are mostly to do with tidying up the logic and also just making our app a little

15
01:19.490 --> 01:20.930
bit easier to use.

16
01:20.960 --> 01:27.890
You'll notice I've added some extra bits in here, like some asterisks, just to make each section when

17
01:27.890 --> 01:31.160
they lose, or when they win a little bit more obvious.

18
01:31.160 --> 01:38.930
And in Step 4, I want you to give the user some feedback to improve our user experience.

19
01:38.930 --> 01:44.540
So if the user has entered a letter that they've already guessed, we need to tell them, "You already

20
01:44.540 --> 01:47.810
guessed this." And we'll print that letter and let them know.

21
01:47.810 --> 01:52.010
But we're not going to deduct any lives because I think that's maybe a little bit unfair.

22
01:52.010 --> 01:56.690
So an example is you've already guessed letter a, something like this.

23
01:56.690 --> 02:01.130
Now TODO Number 5 is on a similar vein.

24
02:01.130 --> 02:05.960
If the letter they guessed is not in the chosen_word, we're going to print it out and let them know.

25
02:05.960 --> 02:07.460
So give them some feedback.

26
02:07.460 --> 02:10.100
For example, "You guessed d, that's not in the word.

27
02:10.100 --> 02:14.210
You lose a life." Just so they know exactly what's going on in the game.

28
02:14.780 --> 02:22.040
And the final TODO, TODO  Number 6, is to update this print statement so that we give the user an indication

29
02:22.040 --> 02:23.900
of how many lives they have left.

30
02:23.900 --> 02:27.500
So it'll be 4 / 6 lives left, then 3 / 6 lives left,

31
02:27.500 --> 02:35.930
So  each time we rerun our while loop, they know how close they are to the end and give them a little

32
02:35.930 --> 02:39.440
bit more danger and anticipation.

33
02:39.440 --> 02:45.080
So these TODOs are a lot easier than previous ones, and I think you'll be able to tackle them just

34
02:45.080 --> 02:45.440
fine.

35
02:45.440 --> 02:48.860
So pause the video and complete your project.

36
02:51.860 --> 02:52.430
Okay.

37
02:52.430 --> 02:55.430
Let's start with TODO Number one.

38
02:55.430 --> 03:00.160
So you might remember that we can simply import any module,

39
03:00.160 --> 03:03.880
and this module is called hangman_words.

40
03:03.880 --> 03:05.470
And it looks like this,

41
03:05.500 --> 03:09.490
it contains just a single list called word_list.

42
03:09.490 --> 03:11.290
So we can do it in two ways,

43
03:11.290 --> 03:14.860
we can do it the usual way, which is how we imported random.

44
03:14.860 --> 03:19.720
So we import the module which is hangman_words,

45
03:19.720 --> 03:25.210
and then we can use it inside our file.

46
03:25.210 --> 03:32.650
So here when we're using word_list, to get rid of this red underline, we can say hangman_words.word_list

47
03:32.650 --> 03:33.130
.

48
03:33.130 --> 03:37.210
And then Python will be able to find this file because it's imported.

49
03:37.210 --> 03:40.270
And then find this word_list that we need.

50
03:40.270 --> 03:48.070
Another way of doing imports is a way where you can avoid using these dots and makes the code just a

51
03:48.070 --> 03:49.090
little bit tighter.

52
03:49.090 --> 03:53.620
So instead of just writing import hangman_words, you can say from

53
03:55.750 --> 04:03.700
this module called, hangman_words, import this one thing, which is the words_list.

54
04:03.730 --> 04:08.710
Now of course, if you have lots of things to import from a particular file or module, then this is

55
04:08.710 --> 04:10.150
going to be a little bit painful,

56
04:10.150 --> 04:17.950
but in our case, when there's only very few and we only want to import one or two things, then this syntax

57
04:17.950 --> 04:20.860
can make it a lot lighter.

58
04:20.860 --> 04:26.680
And it means we don't have to write hangman_words.word_list every time we want to use it, which

59
04:26.680 --> 04:28.120
can be a bonus.

60
04:28.120 --> 04:30.700
Now, of course, you might not have seen it this way.

61
04:30.700 --> 04:34.090
So if you complete it the other way, it's also perfectly valid,

62
04:34.090 --> 04:37.870
and in most situations, that's exactly how you want to do it.

63
04:37.900 --> 04:38.380
All right.

64
04:38.380 --> 04:42.430
Let's go ahead and knock off TODO Number 2, and TODO Number 3.

65
04:42.430 --> 04:46.600
So TODO Number 2 we just need to do the same thing and import.

66
04:46.600 --> 04:52.960
So from hangman_art, we're going to import the list called stages,

67
04:52.960 --> 04:57.010
and then that gets rid of that error right there.

68
04:57.010 --> 05:05.050
And TODO Number 3 we need to import the logo which we've got right here called, logo.

69
05:05.050 --> 05:09.790
And this just means that when we start up our game it will display a nice little logo.

70
05:09.790 --> 05:13.870
So we'll just print and we're going to print the logo.

71
05:13.870 --> 05:16.540
And then we're going to import the logo.

72
05:16.540 --> 05:20.440
So we can also add a new from whatever,

73
05:20.440 --> 05:25.660
but it's actually much easier to add a comma and just add logo afterwards.

74
05:25.660 --> 05:30.880
So this way you save yourself a little bit of code.

75
05:30.880 --> 05:35.170
But if you wrote it like this, hangman_art.logo,

76
05:35.200 --> 05:37.960
that is also perfectly valid.

77
05:38.050 --> 05:43.720
Now Step Number 4 is when the user has entered a letter they've already guessed,

78
05:43.720 --> 05:45.940
we're going to print the letter and let them know.

79
05:45.940 --> 05:50.410
So this is the example and I'm going to use it.

80
05:50.410 --> 05:57.940
So if the guess, so the letter that we're guessing right now is in,

81
05:57.940 --> 06:04.450
so the 'in' keyword, to check if it exists inside the set of correct_letters,

82
06:04.450 --> 06:09.160
well, then that means that they previously already guessed that letter.

83
06:09.160 --> 06:16.870
So we need to let them know, print that they've already guessed that letter. So we can copy this

84
06:16.870 --> 06:24.190
line and paste it in here and use an f-string to insert the guess right there.

85
06:24.280 --> 06:28.480
So now if we go ahead and run our file as it is, you'll see,

86
06:28.510 --> 06:31.810
firstly, we get our nice little Hangman logo printing.

87
06:31.810 --> 06:38.440
And I've got the chosen_word still printing right now because otherwise, it's impossible to test this.

88
06:38.440 --> 06:41.710
I'm not good enough at Hangman to be able to do this.

89
06:42.010 --> 06:45.910
So we've got our word to guess, and then we have to guess a letter,

90
06:45.910 --> 06:51.490
So I'm going to guess r, and you can see if I guess r again I don't lose a life,

91
06:51.490 --> 06:57.670
and it just tells me that I've already guessed r. So that works as expected.

92
06:57.970 --> 07:04.360
Moving on to the next TODO if the letter is not in the chosen_word, then we're going to print it out

93
07:04.360 --> 07:05.800
and let them know.

94
07:05.800 --> 07:11.020
So it's important to give user feedback because that improves the user experience.

95
07:11.020 --> 07:17.860
Now there's a lot of science and art to user experience, but it's good to start thinking about this

96
07:17.860 --> 07:20.470
even as we're learning the basics of Python.

97
07:21.400 --> 07:30.580
So if the letter is not in the chosen_word, so that is under this if statement, then we're going to

98
07:30.580 --> 07:32.860
reduce their lives by 1,

99
07:32.860 --> 07:36.280
but we're also going to add this print statement.

100
07:36.280 --> 07:39.640
So I'm going to copy the example as it is.

101
07:39.640 --> 07:48.160
And then I'm going to paste it into this if statement and replace this with an f-string, which is the

102
07:48.160 --> 07:56.920
{guess}. And very thankfully, Python automatically adds this f in here because I'm using PyCharm and

103
07:56.920 --> 07:59.470
it's a really, really helpful code editor.

104
07:59.470 --> 08:02.800
But just be aware that that's what's happening just now.

105
08:03.160 --> 08:10.780
Moving on to TODO Number 6, we're going to update this part, this print statement to tell the user

106
08:10.780 --> 08:17.020
how many lives they have left so that each time the while loop runs, they can see how many lives they

107
08:17.020 --> 08:19.840
have left 4/6, 3/6, 2/6,

108
08:19.840 --> 08:25.720
give them a little bit of a feeling of impending doom as any good game should do.

109
08:25.720 --> 08:27.760
If you play Elden Ring, you'll know what I mean.

110
08:27.940 --> 08:32.890
And inside here we're going to insert the variable called lives.

111
08:32.890 --> 08:35.200
And again, this should be an f-string.

112
08:35.200 --> 08:39.540
And we only have one more TODO left before we finish this game.

113
08:39.540 --> 08:45.390
So update this last print statement to give them the correct_word that they're trying to guess.

114
08:45.390 --> 08:48.900
It's never good to frustrate your users too much.

115
08:49.050 --> 08:55.350
I'm looking at you from software, the people who make Elden Ring and making it really difficult to

116
08:55.350 --> 08:56.640
pass all the levels,

117
08:57.030 --> 09:03.480
but here we want to be able to give the user the word that they've been trying to guess all along.

118
09:03.480 --> 09:07.920
So even if they don't get it and they lose, they at least know what it was.

119
09:07.920 --> 09:10.980
So we're going to again insert this as an f-string.

120
09:10.980 --> 09:20.490
And I'm just going to copy the format here in the TODO, telling them that it was this particular word,

121
09:20.490 --> 09:26.040
replacing this with the f-string, and my variable is called chosen_word.

122
09:26.040 --> 09:32.250
And now I have completed the entire project of Hangman.

123
09:32.250 --> 09:35.400
Now, I'm not saying that there's no more improvement.

124
09:35.400 --> 09:41.100
There's a lot of things that you could do to improve Hangman, and this is by no means a fully-fledged

125
09:41.100 --> 09:42.030
game.

126
09:42.330 --> 09:50.700
It is just an exercise to be able to test and revise some of the concepts you've learned about

127
09:50.700 --> 09:55.020
Python up to this point, and hopefully it was fun building it,

128
09:55.020 --> 09:59.070
and hopefully, you're going to have some fun playing it and showing it to friends.

129
09:59.100 --> 10:05.160
Now, there are a ton of more improvements that you can make to this game, and if you're up for it,

130
10:05.160 --> 10:09.570
then you can of course go ahead and make it to your heart's desire.

131
10:09.570 --> 10:12.570
Change it, or update the user experience,

132
10:12.570 --> 10:14.130
change the wording,

133
10:14.130 --> 10:19.770
give it more functionality, different functionality, whatever it is you want to do, the world is

134
10:19.770 --> 10:20.670
your oyster.

135
10:20.670 --> 10:25.410
But hopefully, you've managed to understand all of the code up to now,

136
10:25.410 --> 10:32.580
and if you don't, just try deleting everything and starting from the beginning until you make sure

137
10:32.580 --> 10:38.520
that every single concept in here that you've reviewed, either you've gone back to previous videos

138
10:38.520 --> 10:41.250
or you've looked up documentation,

139
10:41.250 --> 10:44.490
as long as everything makes sense before you proceed.

140
10:44.520 --> 10:52.200
I know it's really tempting to keep to the one day,  a day kind of format, but the more important

141
10:52.200 --> 10:56.490
thing is that you're actually learning valuable skills and you're not just skipping ahead.

142
10:56.490 --> 11:02.670
So I hope this was fun, and I hope you'll look forward to building more stuff with me in future lessons,

143
11:02.670 --> 11:05.580
but right now, take a rest and well done.