1
00:00:00,090 --> 00:00:01,020
How did you get on?

2
00:00:01,680 --> 00:00:05,070
Hopefully you managed to complete the project and you're just here

3
00:00:05,070 --> 00:00:07,470
to check through the solution. Now,

4
00:00:07,530 --> 00:00:10,110
if you haven't given it a proper go and I mean,

5
00:00:10,110 --> 00:00:14,400
at least an hour at tackling this, then go back, keep going.

6
00:00:14,640 --> 00:00:17,460
It's only when you struggle that you actually get better.

7
00:00:17,790 --> 00:00:19,800
You can watch me type code all day long,

8
00:00:19,800 --> 00:00:21,630
and it's not going to make you a better programmer.

9
00:00:21,930 --> 00:00:25,560
So I know you're going to try really hard and I want you to try really,

10
00:00:25,560 --> 00:00:30,150
really extra hard and make sure that you can complete each of these projects by

11
00:00:30,150 --> 00:00:34,230
yourself. I mentioned that this one doesn't have a starting repl,

12
00:00:34,230 --> 00:00:38,610
so we're gonna start out with our own and you can call it anything you want,

13
00:00:38,610 --> 00:00:42,930
but I'm going to call it guess-the-number game.

14
00:00:43,860 --> 00:00:46,290
Now, when you're creating a new project from scratch

15
00:00:46,320 --> 00:00:47,580
like we're doing right now,

16
00:00:48,030 --> 00:00:51,420
it can seem a little bit intimidating staring at a blank screen.

17
00:00:51,900 --> 00:00:56,820
The first thing to do is to just divide the project into the individual

18
00:00:56,820 --> 00:00:59,910
components. So while you're playing this game,

19
00:01:00,030 --> 00:01:03,930
you can see that there are different bits of it that we need to complete right?

20
00:01:04,379 --> 00:01:08,280
The first one is printing all the starting parts of the game,

21
00:01:08,640 --> 00:01:12,390
the next one is trying to figure out how to set this difficulty level,

22
00:01:13,140 --> 00:01:15,960
getting the user to type in a word easy or hard,

23
00:01:16,320 --> 00:01:21,320
and then changing the number of attempts based on that. We can already start

24
00:01:21,870 --> 00:01:26,870
thinking about maybe we need to make a function to set difficulty,

25
00:01:28,380 --> 00:01:31,290
and then we need to let the user make a guess, right?

26
00:01:31,320 --> 00:01:35,280
So they're gonna guess a number. So let's say I'm going to type in 50,

27
00:01:35,820 --> 00:01:39,660
and then we have to somehow check that guess against the number that was

28
00:01:39,660 --> 00:01:44,340
originally selected and then check to see if it's too low or too high.

29
00:01:45,030 --> 00:01:48,390
So now we have some additional parts of our program, right?

30
00:01:48,390 --> 00:01:53,390
We need some sort of way of choosing a random number between 1 and 100.

31
00:01:57,120 --> 00:02:00,000
So that's the first thing that's probably going to happen in our code.

32
00:02:00,300 --> 00:02:02,970
We need to have a function to set difficulty,

33
00:02:03,240 --> 00:02:06,270
we probably need some sort of functionality to, um,

34
00:02:06,300 --> 00:02:10,470
let the user guess a number.

35
00:02:12,450 --> 00:02:17,450
And we'll also want to check that number that they've guessed against the number

36
00:02:18,660 --> 00:02:21,750
that we originally had to see if it's too high or too low.

37
00:02:21,900 --> 00:02:26,040
So probably a function to check user's

38
00:02:26,100 --> 00:02:30,750
guess against the actual answer.

39
00:02:31,560 --> 00:02:35,130
Now notice how every single time I make a guess,

40
00:02:35,190 --> 00:02:37,050
my number of attempts go down.

41
00:02:37,410 --> 00:02:42,410
So we also need to track that right? Track the number of turns and reduce by one

42
00:02:45,750 --> 00:02:47,460
if they get it wrong.

43
00:02:48,870 --> 00:02:53,760
Now it's looping back again to this stage where we're making another guess.

44
00:02:53,790 --> 00:02:56,640
So if 50 was too high, then maybe we'll guess 25.

45
00:02:57,240 --> 00:03:01,450
We have to have some sort of way of repeating this part of the code, right?

46
00:03:02,920 --> 00:03:07,510
We're going to repeat it as long as they have attempts left and they are wrong.

47
00:03:08,350 --> 00:03:10,660
Now, if however they get it right,

48
00:03:11,020 --> 00:03:14,800
so I think I have a good chance of getting it right. So if 25 is still too high,

49
00:03:14,800 --> 00:03:18,760
let's try 12. And there we go. When I get the answer right,

50
00:03:18,760 --> 00:03:20,980
it tells me, you got it. The answer was 12.

51
00:03:21,520 --> 00:03:25,840
So now we've got some of the basic parts of our code down in comments,

52
00:03:26,230 --> 00:03:29,590
and we can start thinking about how to write it out, right?

53
00:03:30,220 --> 00:03:32,350
Let's start by tackling the easiest one,

54
00:03:32,350 --> 00:03:35,410
which is choosing a random number between one and a hundred.

55
00:03:35,950 --> 00:03:40,950
All you have to do for this one is just to import the random module, or even

56
00:03:41,290 --> 00:03:46,150
better yet from the random module, import the randint function.

57
00:03:47,410 --> 00:03:48,910
Now inside our code,

58
00:03:48,940 --> 00:03:53,940
we can simply just use randint. We can say the answer is equal to a randint

59
00:03:55,780 --> 00:04:00,430
that is between 1 and 100. And of course remember that rand

60
00:04:00,430 --> 00:04:02,890
int includes a and b,

61
00:04:02,890 --> 00:04:05,950
so both 1 and 100 can show up.

62
00:04:07,090 --> 00:04:10,810
In addition to this, we probably have to give the user some of these messages.

63
00:04:10,810 --> 00:04:13,060
So I'm just going to copy some of these messages.

64
00:04:13,540 --> 00:04:17,769
So I'm going to print 'Welcome to the guessing game.'

65
00:04:18,339 --> 00:04:20,440
And also I'm going to print that

66
00:04:20,440 --> 00:04:25,390
I'm thinking of a number between 1 and 100, like that.

67
00:04:25,990 --> 00:04:29,080
So now we've got an answer that they need to guess,

68
00:04:29,260 --> 00:04:31,810
and we've got a little bit of starting text for them.

69
00:04:32,350 --> 00:04:36,670
The next easiest thing in this is to figure out a way of letting the user

70
00:04:36,700 --> 00:04:40,120
guess a number. So we know that we'll need an input to

71
00:04:40,150 --> 00:04:45,040
ask them to make a guess, so we can copy this part.

72
00:04:45,190 --> 00:04:48,130
And we can use that as the prompt,

73
00:04:48,670 --> 00:04:52,810
and then we're going to have to save their guess and convert it to an integer.

74
00:04:53,470 --> 00:04:58,060
So they're going to be typing a number here, right? And we'll call this guess.

75
00:04:58,960 --> 00:05:03,960
Now the user is able to guess a number and we now have to figure out some sort of

76
00:05:05,020 --> 00:05:09,970
way of checking the user's guess against the actual answer.

77
00:05:10,750 --> 00:05:13,450
This is probably a good time to create a function.

78
00:05:13,630 --> 00:05:18,310
So why don't we go up to the very top and create our function here.

79
00:05:18,910 --> 00:05:20,770
We'll call this check_answer,

80
00:05:21,700 --> 00:05:26,410
and we're gonna pass in the user's guess and the actual answer in order to

81
00:05:26,410 --> 00:05:30,340
achieve this checking functionality. So in this case,

82
00:05:30,340 --> 00:05:35,340
we're going to check to see if the guess is greater than the answer,

83
00:05:36,010 --> 00:05:40,300
well, in this case, it's too high, right? So we'll just print too high.

84
00:05:41,470 --> 00:05:44,920
Now, what if the guess was actually less than the answer,

85
00:05:45,220 --> 00:05:49,360
still not equal to the answer, but in this case it's too low.

86
00:05:50,290 --> 00:05:53,530
So now the final condition is they actually got it right.

87
00:05:53,530 --> 00:05:57,040
So you could write elif guess is equal to answer,

88
00:05:57,350 --> 00:06:00,650
but you could also just use else because there's no other possibilities.

89
00:06:01,340 --> 00:06:04,970
This else statement catches the moment when they actually get it right.

90
00:06:05,900 --> 00:06:08,690
So in this case, we're going to print what we have here,

91
00:06:08,930 --> 00:06:10,790
and then we'll get to print out the answer. Of course,

92
00:06:10,790 --> 00:06:14,690
we're going to replace that with whatever the actual answer is and turn this

93
00:06:14,690 --> 00:06:18,860
into an fstring. So now that's that step done.

94
00:06:19,310 --> 00:06:22,610
And let's think about what else is easy that we can knock off the list.

95
00:06:23,120 --> 00:06:27,080
Let's think of function to set the difficulty. So up here,

96
00:06:27,110 --> 00:06:29,420
in addition to our check_answer function,

97
00:06:29,720 --> 00:06:34,720
I'm going to create this other function, some way of setting the difficulty.

98
00:06:35,270 --> 00:06:37,580
So let's call this set_difficulty,

99
00:06:38,480 --> 00:06:41,000
and it's probably not going to take any inputs.

100
00:06:41,030 --> 00:06:45,800
It's just going to do some sort of action. So when we first start off the game,

101
00:06:45,950 --> 00:06:48,980
it already asks us to choose a difficulty.

102
00:06:48,980 --> 00:06:53,980
So I'm going to copy this text and I'm going to put that into an input,

103
00:06:55,310 --> 00:06:58,280
choose a difficulty easy or hard.

104
00:06:58,970 --> 00:07:03,380
And then we're going to save this probably into a variable,

105
00:07:03,410 --> 00:07:08,090
which I'll call level, so the level of difficulty that they want. Now,

106
00:07:08,120 --> 00:07:13,120
if this level that they chose was equal to easy,

107
00:07:13,820 --> 00:07:14,660
well, in this case,

108
00:07:14,690 --> 00:07:19,690
we want to set the number of attempts that they have to 10.

109
00:07:20,180 --> 00:07:23,210
So they have 10 attempts. But on the other hand,

110
00:07:23,900 --> 00:07:27,800
if they chose hard, then they only have 5 attempts.

111
00:07:28,280 --> 00:07:33,280
So this is probably a really good time for creating a global constant.

112
00:07:34,370 --> 00:07:37,250
So we already saw this in the previous lesson on scope,

113
00:07:37,730 --> 00:07:42,110
but this is a really nice thing because it means if we create this global

114
00:07:42,110 --> 00:07:46,760
constant at the very top, then if we want to switch around how our game works,

115
00:07:46,910 --> 00:07:51,860
we can really easily access these constant by just scrolling through the top of

116
00:07:51,860 --> 00:07:54,920
the page. Let's call the first one, um,

117
00:07:54,980 --> 00:07:59,690
EASY_LEVEL_TURNS, so the number of terms you get on easy,

118
00:07:59,990 --> 00:08:01,130
which is going to be 10.

119
00:08:01,580 --> 00:08:06,410
And then we'll have HARD_LEVEL_TURNS

120
00:08:06,860 --> 00:08:08,420
and this is going to be equal to 5.

121
00:08:09,110 --> 00:08:13,370
Now I've got these two constants which is global

122
00:08:13,430 --> 00:08:17,330
and I can use it inside any of my functions or basically anywhere on this

123
00:08:17,330 --> 00:08:19,910
page. If the level is easy,

124
00:08:20,060 --> 00:08:25,060
I'm going to set the number of turns to equal the EASY_LEVEL_TURNS.

125
00:08:26,750 --> 00:08:29,960
So this is going to be a number right? Else,

126
00:08:29,990 --> 00:08:31,910
so if they chose hard instead,

127
00:08:32,360 --> 00:08:36,830
then turns should probably be the HARD_LEVEL_TURNS. Now,

128
00:08:36,860 --> 00:08:41,390
setting it here is no use. I actually need it inside my game.

129
00:08:42,320 --> 00:08:44,300
So now going back in my game,

130
00:08:44,330 --> 00:08:47,690
I need to let the user know how many number of attempts,

131
00:08:47,690 --> 00:08:50,840
how many turns they actually get. To start off

132
00:08:50,870 --> 00:08:55,710
I'm going to use a print statement that just prints the number attempts they

133
00:08:55,710 --> 00:08:58,560
have remaining. And it's not always going to be 5.

134
00:08:58,560 --> 00:09:01,800
Sometimes it's going to be 5, sometimes it's going to be 10.

135
00:09:02,220 --> 00:09:07,020
So what I'll need is some sort of way of tracking this number of turns,

136
00:09:07,020 --> 00:09:11,400
right? But how do we know how many turns they get? Well,

137
00:09:11,400 --> 00:09:16,260
it depends on what they chose here, right? If they had chosen hard,

138
00:09:16,320 --> 00:09:20,700
then turns should be equal to the HARD_LEVEL_TURNS. If they chose easy,

139
00:09:20,700 --> 00:09:22,410
it should be equal to the EASY_LEVEL_TURNS.

140
00:09:22,980 --> 00:09:25,860
So instead of using this as some sort of global variable

141
00:09:26,130 --> 00:09:30,840
which I have to change inside here, which would require me saying global,

142
00:09:31,050 --> 00:09:32,790
and then turns so that

143
00:09:32,790 --> 00:09:37,080
I can access this particular variable and change it inside

144
00:09:37,080 --> 00:09:39,120
these steps, it'd would be much,

145
00:09:39,150 --> 00:09:44,150
much better if I just returned these values as an output from this function.

146
00:09:45,930 --> 00:09:47,760
If this is set as return,

147
00:09:47,850 --> 00:09:51,600
then I can simply call this function set_difficulty,

148
00:09:52,050 --> 00:09:56,350
which is going to trigger this input. And then once the user has set it,

149
00:09:56,520 --> 00:10:00,540
then the output is going to be the actual number of turns they have.

150
00:10:00,900 --> 00:10:05,340
And I can now use it inside this print statement to show them how many turns

151
00:10:05,340 --> 00:10:07,710
they have. Now.

152
00:10:07,800 --> 00:10:10,350
I've already written quite a bit of code actually,

153
00:10:10,410 --> 00:10:15,410
and it's probably a good time to run the code. While I'm testing my code,

154
00:10:15,600 --> 00:10:18,960
it's probably a good idea to actually see what the answer is.

155
00:10:19,290 --> 00:10:23,730
So we can add a bit of testing code that just prints out what the actual answer

156
00:10:23,730 --> 00:10:26,250
was selected as. And if we run the code

157
00:10:26,250 --> 00:10:30,120
now you can see we've got the starting part pretty much down.

158
00:10:30,510 --> 00:10:31,920
Welcome to the Number Guessing Game.

159
00:10:31,920 --> 00:10:33,990
I'm thinking have a number between 1 and 100,

160
00:10:34,290 --> 00:10:37,650
and then we've got our little hint here. The actual answer is 63.

161
00:10:38,190 --> 00:10:39,930
So we have to make a guess.

162
00:10:40,110 --> 00:10:44,730
And I've really noticed that that input needs a space here so that I'm not

163
00:10:44,730 --> 00:10:48,960
writing right next to the colon. So let's say I guessed 50.

164
00:10:49,920 --> 00:10:52,620
Now it's asking us to choose a difficulty.

165
00:10:52,950 --> 00:10:54,990
So I've already got a little bit of a problem here.

166
00:10:55,020 --> 00:10:58,230
I should actually have this before the other part.

167
00:10:58,770 --> 00:11:02,520
So I should choose the difficulty and then make a guess.

168
00:11:02,880 --> 00:11:07,740
So now let's stop and run and see if we fix that bug. And you can see

169
00:11:07,740 --> 00:11:11,070
yep, indeed it asks us for a difficulty first.

170
00:11:11,430 --> 00:11:12,840
So I'm going to choose easy

171
00:11:13,140 --> 00:11:17,100
and it tells me that I've got 10 attempts remaining and then to make a guess.

172
00:11:17,580 --> 00:11:22,260
And if I choose hard instead, then it tells me I've got 5 attempts.

173
00:11:22,680 --> 00:11:24,990
Now, if while you are play testing this game

174
00:11:24,990 --> 00:11:28,260
and you realize that actually 5 is not enough, then all you have to do is

175
00:11:28,260 --> 00:11:29,580
scroll to the very top of the game,

176
00:11:29,970 --> 00:11:33,630
change this one global variable here,

177
00:11:34,080 --> 00:11:37,800
and all your changes, you know, will be reflected in the code below.

178
00:11:39,270 --> 00:11:43,530
Now let's get back to our game. Let's say I make a guess and our code ends,

179
00:11:43,560 --> 00:11:46,470
right? Even though I've got my check_answer,

180
00:11:46,620 --> 00:11:48,750
it's not actually being called anywhere.

181
00:11:48,930 --> 00:11:52,170
So it lets go ahead and call that function, check_answer

182
00:11:52,710 --> 00:11:56,110
and then we can pass in the user's guess that they made at this step,

183
00:11:56,470 --> 00:12:00,370
and we can also pass in the correct answer which comes from here.

184
00:12:01,000 --> 00:12:02,830
So let's run our code again.

185
00:12:06,550 --> 00:12:09,940
So I'm going to choose the easy mode and I'm going to make a guess.

186
00:12:10,570 --> 00:12:14,380
Now 50 is too high because I can actually see the answer,

187
00:12:14,380 --> 00:12:19,030
it's meant to be 32. So that works. But I can't guess again.

188
00:12:19,030 --> 00:12:22,690
It's not letting me. It's actually ended the program.

189
00:12:23,290 --> 00:12:26,950
So we probably have to tackle this particular todo

190
00:12:26,980 --> 00:12:29,320
if we actually want to get the right functionality.

191
00:12:29,710 --> 00:12:34,710
How do we get the guessing functionality to repeat if they get it wrong? So that

192
00:12:35,980 --> 00:12:40,980
as you might've guessed is probably gonna require some sort of while loop. While

193
00:12:41,440 --> 00:12:45,910
they've gotten it wrong, we probably should go and let them make a guess

194
00:12:45,940 --> 00:12:49,690
again. These parts will definitely need to be repeated.

195
00:12:50,380 --> 00:12:52,000
So let's create a while loop.

196
00:12:52,540 --> 00:12:56,860
What are we going to check in our while loop to make sure that it keeps on going?

197
00:12:57,430 --> 00:12:57,970
Well,

198
00:12:57,970 --> 00:13:02,800
the thing that we're trying to aim for is to get the user to guess the actual

199
00:13:02,800 --> 00:13:03,633
answer.

200
00:13:03,820 --> 00:13:08,820
If we set the while condition as while guess is not equal to answer,

201
00:13:09,550 --> 00:13:13,990
then that pretty much guarantees that these lines of code will keep going until

202
00:13:13,990 --> 00:13:18,310
the user actually gets the right answer. Now however,

203
00:13:18,340 --> 00:13:21,940
when we do this, we get an error, undefined name guess.

204
00:13:22,030 --> 00:13:24,550
Well because guess hasn't been declared yet.

205
00:13:25,030 --> 00:13:30,030
So why don't we declare it outside of the while loop and simply just create it as

206
00:13:31,330 --> 00:13:35,980
a variable? So we'll set that to equal zero. Now at the moment,

207
00:13:36,040 --> 00:13:40,660
this is a global variable. This is a global variable. This is a global variable.

208
00:13:40,900 --> 00:13:45,900
So it's probably a good idea to put our game functionality inside its own

209
00:13:47,140 --> 00:13:51,700
function. So we can create a new function here, which I'll call game,

210
00:13:52,360 --> 00:13:57,360
and now we'll need to call and trigger this game inside the global scope

211
00:13:58,480 --> 00:14:00,700
if we actually want this to be carried out.

212
00:14:02,410 --> 00:14:04,360
Now that we've done all of this hard work,

213
00:14:04,420 --> 00:14:07,030
let's run our code and let's try it out.

214
00:14:09,640 --> 00:14:10,960
Lets go for easy.

215
00:14:11,590 --> 00:14:16,590
And I'm going to start off by guessing 50 as always. 50 is too low and I can see

216
00:14:17,290 --> 00:14:20,590
the answer is actually 90. So I'm going to go for the next number.

217
00:14:20,590 --> 00:14:23,740
Lets try a number that is a bit too high. 95, too high.

218
00:14:24,340 --> 00:14:28,690
Now, what if I actually get it right? What if I actually guessed 90? Well,

219
00:14:28,690 --> 00:14:31,240
it tells me you got it. The answer was 90.

220
00:14:32,140 --> 00:14:36,910
So the only thing that we're not really doing is tracking the number of turns so

221
00:14:36,910 --> 00:14:39,940
that we tell them you have 10 attempts, you have 9 attempts,

222
00:14:39,940 --> 00:14:42,430
you have zero attempts. And if they get zero,

223
00:14:42,430 --> 00:14:45,310
then they're going to lose right? Here

224
00:14:45,340 --> 00:14:49,630
we have this print statement that runs only once because it's not inside the

225
00:14:49,630 --> 00:14:52,640
while loop. So let's tackle our final to-do

226
00:14:52,790 --> 00:14:56,120
to track the number of turns and reduce it by one

227
00:14:56,180 --> 00:15:01,100
if they get it wrong. Now, where do we know that the user got it wrong?

228
00:15:01,580 --> 00:15:06,470
Well inside the check answer function. If it's too high or if it's too low,

229
00:15:06,560 --> 00:15:10,700
then we have to deduct the number of turns they have. But if they got it,

230
00:15:10,790 --> 00:15:13,370
then we don't. One way of tackling

231
00:15:13,370 --> 00:15:18,140
this is again creating a global variable called turns,

232
00:15:18,650 --> 00:15:21,200
set it to start off at zero,

233
00:15:22,040 --> 00:15:24,290
and then inside this function,

234
00:15:24,500 --> 00:15:29,500
we simply just say turns -= 1 and also turns -= 1 when

235
00:15:31,160 --> 00:15:36,160
it's too low or too high. Now because this is a global variable

236
00:15:36,830 --> 00:15:41,830
we again would need to say global turns in order to use it inside our function.

237
00:15:43,580 --> 00:15:44,570
But as I mentioned,

238
00:15:44,630 --> 00:15:49,250
this is not a good idea because it means that you're modifying the global

239
00:15:49,250 --> 00:15:51,590
function within a local scope.

240
00:15:52,010 --> 00:15:56,960
How can we achieve the same functionality without having to use a global

241
00:15:56,960 --> 00:15:58,820
variable? Well,

242
00:15:58,850 --> 00:16:03,850
we could instead just pass in the number of turns that we currently have and

243
00:16:04,820 --> 00:16:06,980
then inside our function,

244
00:16:08,060 --> 00:16:13,060
we can return the number of turns minus one. And we can do the same down here.

245
00:16:18,130 --> 00:16:22,120
So now when we call this function check_answer,

246
00:16:22,450 --> 00:16:25,840
we can pass in the current number of turns

247
00:16:26,050 --> 00:16:29,530
which remember it got set by the set_difficulty function,

248
00:16:30,100 --> 00:16:32,650
and we can pass that in into here.

249
00:16:34,360 --> 00:16:38,740
Because it's not immediately obvious that this function actually has an output,

250
00:16:39,430 --> 00:16:43,270
it's probably a good idea to add a docstring to this function.

251
00:16:44,080 --> 00:16:46,330
Remember the three quotes,

252
00:16:46,570 --> 00:16:49,600
and then in between we're going to say checks

253
00:16:49,660 --> 00:16:54,310
answer against guess. Returns

254
00:16:55,930 --> 00:16:59,050
the number of turns remaining.

255
00:17:01,060 --> 00:17:04,060
Now when we actually call check_answer

256
00:17:04,510 --> 00:17:09,510
you can see that this is what actually happens and that we have a return that we

257
00:17:09,910 --> 00:17:10,900
can be aware of.

258
00:17:12,670 --> 00:17:15,250
If this is going to be the number of turns remaining,

259
00:17:15,520 --> 00:17:20,520
then we can save that to this variable turns so that we update this local

260
00:17:21,250 --> 00:17:23,920
variable every time we check the answer.

261
00:17:25,540 --> 00:17:27,790
Now this part is a little bit mind blowing,

262
00:17:27,880 --> 00:17:30,940
so you might actually want to put this through Python

263
00:17:30,940 --> 00:17:32,560
Tutor and step through it

264
00:17:32,560 --> 00:17:37,420
step by step to see what's actually going on and how this turns variable is

265
00:17:37,420 --> 00:17:40,480
being updated by the output from this function.

266
00:17:40,960 --> 00:17:44,620
If you're totally happy with what's going on, then feel free to keep going.

267
00:17:46,390 --> 00:17:48,460
So now let's check out our code.

268
00:17:50,040 --> 00:17:55,040
50 is too high and 20 is too low, and 38 is just right. Now

269
00:17:56,820 --> 00:18:00,510
what if I actually ran my number of turns down to zero?

270
00:18:00,840 --> 00:18:04,350
So let's try hard because this is only going to give us five attempts.

271
00:18:04,830 --> 00:18:08,730
So let's say I guess 50 and then 70,

272
00:18:09,270 --> 00:18:13,920
and I'm just going to put in some random numbers so that I end up with no

273
00:18:13,920 --> 00:18:18,630
remaining guesses. You can see it's actually not stopping me right?

274
00:18:19,080 --> 00:18:23,160
And also for some reason we have a bug where the number of attempts we have

275
00:18:23,160 --> 00:18:27,630
remaining is not actually being printed. So let's address that issue.

276
00:18:27,960 --> 00:18:31,800
Well, if we want it to happen every time and repeat,

277
00:18:32,100 --> 00:18:36,450
it's going to need to go into the while loop. Instead of printing our number of

278
00:18:36,450 --> 00:18:40,980
turns here, why don't we print it here inside the while loop instead?

279
00:18:42,000 --> 00:18:46,680
So let's make sure the indentation is correct and now let's try it again.

280
00:18:50,330 --> 00:18:53,270
So I have 5 attempts remaining. Let's make a guess.

281
00:18:53,660 --> 00:18:58,660
I have 4 attempts remaining. And notice how it's counting it down;

282
00:18:59,120 --> 00:19:02,900
3, 2, 1. This should be my last guest,

283
00:19:02,960 --> 00:19:06,680
but notice how it actually keeps on going. It doesn't stop the game.

284
00:19:07,010 --> 00:19:10,220
I can keep going and it's going into negatives now.

285
00:19:10,880 --> 00:19:14,870
How can we fix this? We need some sort of way of tracking

286
00:19:14,900 --> 00:19:19,640
when this number of turns goes down to zero. When that happens,

287
00:19:19,730 --> 00:19:22,790
we have to stop the game and tell them that they've lost.

288
00:19:23,450 --> 00:19:26,120
Why don't we create an if statement, check

289
00:19:26,120 --> 00:19:31,120
if the number of tons is equal to zero and print something.

290
00:19:32,450 --> 00:19:36,830
Let's check the actual final version. Let's see what happens in this case.

291
00:19:37,250 --> 00:19:39,050
When I run out of gos,

292
00:19:40,400 --> 00:19:44,420
I make a guess, it's two low and it tells me you've run out of guesses.

293
00:19:44,450 --> 00:19:47,780
You lose. We can print that here as well.

294
00:19:49,940 --> 00:19:52,310
But notice how if we run the code as it is,

295
00:19:52,460 --> 00:19:56,990
so let's just keep guessing the same number until I run it down to 1.

296
00:19:57,380 --> 00:20:01,640
So now I make a guess, it tells me you run out of guesses. You lose.

297
00:20:01,910 --> 00:20:05,150
But it's still letting me keep going even though I've lost.

298
00:20:05,810 --> 00:20:09,350
How can we make our game end? Well,

299
00:20:09,380 --> 00:20:13,130
luckily we've got our game inside a function

300
00:20:13,280 --> 00:20:15,470
which is being called at the very beginning.

301
00:20:16,100 --> 00:20:20,450
Now remember that with functions you have the return keyword.

302
00:20:20,960 --> 00:20:25,960
You can return with an output or you can just write return for it to exit and end

303
00:20:27,440 --> 00:20:32,360
the function. So now with that return keyword in place,

304
00:20:32,780 --> 00:20:35,600
once I run out of turns, see what happens.

305
00:20:38,690 --> 00:20:41,960
It stops and it doesn't let me guess any more.

306
00:20:43,820 --> 00:20:45,320
Perfect. We've managed to get that

307
00:20:45,320 --> 00:20:50,320
right. The thing that we're still missing is this prompt for the user to guess

308
00:20:51,340 --> 00:20:53,440
again. Now this is kind of optional.

309
00:20:53,440 --> 00:20:56,350
It depends on your own feel for the user experience.

310
00:20:56,770 --> 00:21:00,430
I think it makes sense to say, 'It's too low. Guess again.' because it's kind of

311
00:21:00,430 --> 00:21:03,130
telling them to do something. Although there's make a guess,

312
00:21:03,160 --> 00:21:07,330
also prompts them to make a guess. So you can have, you don't have to.

313
00:21:07,570 --> 00:21:08,950
If you wanted to have that,

314
00:21:09,370 --> 00:21:14,050
then you probably actually have to check that they still have turns remaining.

315
00:21:14,560 --> 00:21:19,560
And if they do, then make sure that the guess is not equal to the answer,

316
00:21:21,070 --> 00:21:26,070
so they haven't got it right within this turn, and then to print that 'Guess

317
00:21:26,180 --> 00:21:30,520
again'. So now every time they get it wrong,

318
00:21:30,580 --> 00:21:32,770
but they still have remaining guesses

319
00:21:33,100 --> 00:21:37,960
it should say 'Too low. Guess again.' or 'Too high. Guess again.'

320
00:21:38,800 --> 00:21:40,570
But if they actually get it right,

321
00:21:40,600 --> 00:21:44,440
so the answer should be 83, then it says 'You got it.

322
00:21:44,440 --> 00:21:47,800
The answer was 83' without asking them to guess again.

323
00:21:48,760 --> 00:21:51,880
Now we've pretty much completed this code.

324
00:21:52,270 --> 00:21:54,760
Of course you could have added additional art.

325
00:21:54,760 --> 00:21:59,760
So you could have created an art.py and then used the text to in the ASCII

326
00:21:59,950 --> 00:22:03,580
generator to create a ASCII art.

327
00:22:03,630 --> 00:22:04,463
Yeah.

328
00:22:06,480 --> 00:22:08,840
And then back in here we could have imported it.

329
00:22:12,240 --> 00:22:14,790
And we could put that at the very beginning of the game.

330
00:22:15,510 --> 00:22:19,770
But the rest of the customization is basically your own, right?

331
00:22:19,770 --> 00:22:24,770
You're free to do whatever it is that you want to and to change the wording, to

332
00:22:25,020 --> 00:22:26,010
change the text.

333
00:22:26,280 --> 00:22:30,600
The really important part was that you were able to play this game,

334
00:22:30,990 --> 00:22:35,990
understand how it worked and then break down the problem into smaller pieces

335
00:22:36,300 --> 00:22:38,100
that you could tackle one by one.

336
00:22:38,760 --> 00:22:43,740
And if you had trouble actually completing this program by yourself,

337
00:22:44,220 --> 00:22:47,370
then you should have noticed when we were walking through it

338
00:22:47,610 --> 00:22:50,820
when I just broke down that big a problem into smaller chunks

339
00:22:51,120 --> 00:22:55,140
how much easier it became tackling each of the smaller things, right?

340
00:22:55,440 --> 00:23:00,240
Letting the user guess a number or choosing a random number between 1 and 100,

341
00:23:00,480 --> 00:23:02,580
you can definitely do parts of this.

342
00:23:03,120 --> 00:23:06,450
And as you built up the game, towards the end you realized, ah,

343
00:23:06,480 --> 00:23:08,430
this was missing or that might be nice.

344
00:23:08,760 --> 00:23:11,490
Then you could start adding more and more bits to it.

345
00:23:12,330 --> 00:23:15,510
So if you wanted to review the final code that you see here,

346
00:23:15,810 --> 00:23:20,490
you can, of course, head over to the guess-the-number-final Repl.it at this

347
00:23:20,520 --> 00:23:23,010
particular link in the course resources.

348
00:23:23,460 --> 00:23:27,150
But hopefully, you've actually managed to build out your own version and it's

349
00:23:27,180 --> 00:23:31,320
even better than what I've done. And if you're particularly proud of it,

350
00:23:31,470 --> 00:23:35,340
be sure to share it so that the rest of us can take a look and have a play with

351
00:23:35,340 --> 00:23:36,173
it as well.

352
00:23:36,570 --> 00:23:40,350
So I hope you enjoyed the project today and I'll see you tomorrow.

