WEBVTT

0
00:00.110 --> 00:07.010
So we're now ready to tackle our final project of the day, and that's building up a Calculator program.

1
00:07.490 --> 00:12.950
If you head over to the link for the final demo project, you can see how it works.

2
00:12.950 --> 00:18.050
So firstly, it asks us for the first number that we want to calculate with,

3
00:18.050 --> 00:20.060
so let's start with 5.

4
00:20.060 --> 00:27.440
And then it asks us to type to pick an operation +  -  *  /.

5
00:27.440 --> 00:31.560
So let's pick a multiply by typing the asterisk sign.

6
00:31.770 --> 00:34.440
Now it asks for the next number.

7
00:34.440 --> 00:39.630
So let's say we want to multiply 5 by 3,

8
00:39.630 --> 00:41.070
so we'll type 3.

9
00:41.070 --> 00:47.160
And then once I hit Enter it will perform the operation using floating point numbers,

10
00:47.160 --> 00:49.260
and then it will give us 15.0.

11
00:49.260 --> 00:51.810
So 3 * 5 is 15.0,

12
00:51.810 --> 01:01.750
but now it asks us to type 'y' to continue calculating with the previous result, 15.0, or type 'n' to start a new

13
01:01.750 --> 01:02.710
calculation.

14
01:02.710 --> 01:04.540
So let's try the first version

15
01:04.540 --> 01:06.340
first. Let's type y,

16
01:06.340 --> 01:10.150
and now it asks us again to pick an operation.

17
01:10.150 --> 01:12.370
So notice how this is repeated,

18
01:12.370 --> 01:13.720
the same as here,

19
01:13.720 --> 01:15.100
the Step 2 here.

20
01:15.880 --> 01:19.340
So now I'm going to pick a different operation let's say add.

21
01:19.340 --> 01:21.980
And now I'm working with 15.

22
01:21.980 --> 01:26.360
So 15 plus, let's say 15 + 5,

23
01:26.360 --> 01:27.950
and let's hit Enter.

24
01:27.950 --> 01:33.680
And now it calculates our final result which is 15 + 5 = 20.0.

25
01:33.860 --> 01:43.170
So notice how in this version it took the result of the previous calculation as the first number instead

26
01:43.170 --> 01:44.580
of asking us for it.

27
01:44.580 --> 01:51.180
And then we picked an operation, and then we added the second number and it performed the calculation.

28
01:51.180 --> 01:55.380
So now we can continue working with the previous result,

29
01:55.380 --> 01:59.820
or we can type 'n' to start a new calculation.

30
01:59.820 --> 02:06.400
And again we loop back to the beginning, and we can start typing our first number, our second number,

31
02:06.400 --> 02:07.240
et cetera.

32
02:07.630 --> 02:10.090
So that's the goal for the final project.

33
02:10.090 --> 02:15.970
But before you do that, there's something I want to show that you can do with functions.

34
02:15.970 --> 02:20.410
So here I've created a function with the name add().

35
02:20.440 --> 02:28.660
It has two inputs n1 and n2 and it outputs or returns n1 + n2.

36
02:28.690 --> 02:35.670
So you'll notice this is one of the pieces of functionality that's going to be used in our calculator,

37
02:35.670 --> 02:42.210
how to pass in two numbers to perform the addition functionality and then output the result.

38
02:42.240 --> 02:48.870
Now we've seen functions declared like this before, but what you might not have seen is the ability

39
02:48.870 --> 02:52.800
to store a function as a value of a variable.

40
02:52.800 --> 02:53.880
So what does it mean?

41
02:53.880 --> 03:00.070
Well, let's say I decided that adding numbers was my_favourite_operation.

42
03:01.060 --> 03:06.160
Now this is a variable name and you've seen us create variables like this lots of times, right?

43
03:06.160 --> 03:14.110
I could say my_favourite_operation = 3, or my_favourite_operation = "Hello", I can add strings,

44
03:14.110 --> 03:24.320
I can add various values to the right-hand side of the equal sign to add a value to a named variable.

45
03:24.350 --> 03:29.450
Now, what you might not have seen is you can also do this with functions.

46
03:29.450 --> 03:37.790
However, if we want to store a function as a value, we can't add the parentheses because as I mentioned

47
03:37.790 --> 03:42.440
before, we trigger a function with the parentheses.

48
03:42.450 --> 03:50.100
The parentheses are also obviously for adding in inputs, but if we just want to store a function to

49
03:50.100 --> 03:57.900
another name, give it another reference, then we would do it like this without the parentheses, only

50
03:57.900 --> 04:01.020
with the name of the function.

51
04:01.110 --> 04:09.040
So now what I can do is because that add() function is now stored under a new reference, my_favourite_operation

52
04:09.040 --> 04:10.150
,

53
04:10.150 --> 04:15.130
I can now trigger that function as if I was using the add() function.

54
04:15.130 --> 04:17.890
So 2 and then comma 3.

55
04:17.890 --> 04:24.130
And if I go ahead and print out the output of this result you'll see I get 5.

56
04:24.850 --> 04:26.050
So this is pretty neat,

57
04:26.050 --> 04:30.610
and it's going to be pretty important in order to create our calculator.

58
04:30.620 --> 04:34.820
So now I want you to perform the first challenge.

59
04:34.820 --> 04:38.030
And I'm going to go through the result of the first challenge,

60
04:38.030 --> 04:42.590
so we set you up correctly for tackling this project.

61
04:43.040 --> 04:54.800
So the goal is to first I want you to do this; I want you to write out the other three functions,

62
04:55.290 --> 05:03.210
it's going to be subtract(), multiply() and divide().

63
05:03.390 --> 05:11.910
So based on what you know about functions with inputs and outputs, and also obviously these three mathematical

64
05:11.910 --> 05:18.390
operations, create the other three functions: subtract() multiply() and divide().

65
05:18.420 --> 05:19.870
Pause the video now.

66
05:24.280 --> 05:28.810
Okay, let's go through this because this should be relatively easy to do.

67
05:28.810 --> 05:31.210
So let's first create subtract.

68
05:31.210 --> 05:34.480
And we again are going to use n1 and n2.

69
05:34.480 --> 05:37.570
And in this case we're going to return n1.

70
05:37.570 --> 05:43.460
The first number subtract the second number, so n1 - n2.

71
05:43.550 --> 05:48.200
And then we're going to create multiply again n1, n2,

72
05:48.200 --> 05:52.550
and we're going to return n1 multiply,

73
05:52.550 --> 05:59.000
so asterix, n2. And finally we're going to create divide,

74
05:59.000 --> 06:02.360
and we're going to write n1 and n2,

75
06:02.360 --> 06:08.340
and we're going to return n1 divide, which is the forward slash, n2 (n1/n2).

76
06:08.370 --> 06:09.120
So there we have it.

77
06:09.120 --> 06:12.450
We've got the other three functions created.

78
06:12.540 --> 06:16.620
Now PyCharm is screaming at me because I don't have enough spaces between the functions,

79
06:16.620 --> 06:17.910
but I'm going to write a lot more code,

80
06:17.910 --> 06:20.400
so I really want it to be compact.

81
06:20.400 --> 06:22.590
So we're just going to ignore that for now.

82
06:22.830 --> 06:36.170
Now the next challenge I have for you is to add these four functions into a dictionary as the values,

83
06:36.170 --> 06:38.510
so the dictionary will have,

84
06:38.510 --> 06:41.270
the keys of this,

85
06:42.950 --> 06:48.410
+ is one key, - is another key,

86
06:48.410 --> 06:52.370
*, and finally, /.

87
06:52.370 --> 06:54.620
So these are the four keys.

88
06:54.620 --> 07:00.690
So pause the video and see if you can figure out, remembering what you learned about dictionaries in

89
07:00.690 --> 07:10.080
previous lessons, how to store each of these functions as a value in a dictionary with these four keys,

90
07:10.080 --> 07:15.630
and obviously, storing each function under the corresponding key.

91
07:15.660 --> 07:19.500
Pause the video and see if you can complete this challenge.

92
07:25.660 --> 07:28.180
Okay, let's go through the solution for this.

93
07:28.180 --> 07:32.800
Let's say I decide to create a dictionary called operations,

94
07:32.800 --> 07:37.330
and I'm going to save this as a dictionary.

95
07:37.330 --> 07:42.910
So I open up my curly braces to signify that I'm creating a dictionary.

96
07:42.910 --> 07:46.250
Now I mentioned that there are going to be four keys, right?

97
07:46.250 --> 07:52.160
So "+", "-", "*", and "/".

98
07:52.910 --> 08:00.140
Now relative to each of these keys I'm going to add the corresponding values,

99
08:00.140 --> 08:03.020
and I do that using the colon symbol.

100
08:03.020 --> 08:08.460
So the value for the key, "+" is going to be the function add.

101
08:08.460 --> 08:10.860
And then I add a comma.

102
08:10.860 --> 08:15.600
And I can do this for all four of my functions.

103
08:15.600 --> 08:21.150
Again remembering not to trigger the functions because we're storing it and we're not using it,

104
08:21.150 --> 08:23.850
so we don't want the parentheses.

105
08:25.020 --> 08:28.470
So let me finish that for the rest of them.

106
08:29.190 --> 08:32.440
And finally for divide.

107
08:33.820 --> 08:43.900
So now we've got all four of these functions for our calculator, stored inside a dictionary called operations.

108
08:44.110 --> 08:52.630
Now, just as we're able to trigger a function that's stored under a variable name, as you saw before,

109
08:52.630 --> 08:58.760
we can do the same with these functions stored inside our dictionary.

110
08:58.910 --> 09:08.330
So the final TODO that, I'm going to ask you to do before we let you go and do some independent work,

111
09:08.330 --> 09:19.130
building this project is to use the dictionary operations to perform the calculations.

112
09:19.340 --> 09:22.890
So I want you to try out multiply.

113
09:23.550 --> 09:28.380
And I want you to multiply 4 by 8,

114
09:28.380 --> 09:32.580
and to do this using the dictionary.

115
09:33.180 --> 09:39.990
So see if you can figure out how to do this using what we've covered so far in today's lesson,

116
09:39.990 --> 09:48.640
and also your knowledge of how to use dictionaries and how to access their values through their keys.

117
09:48.670 --> 09:55.210
Pause the video if you need a hint, go back to the lesson on dictionaries and see if you can figure

118
09:55.210 --> 09:58.000
it out by yourself without watching the solution.

119
09:58.690 --> 10:00.010
Pause the video now.

120
10:04.090 --> 10:12.110
Okay, so you might remember that the way that we access a value from a dictionary is we first tap into

121
10:12.110 --> 10:13.490
the name of the dictionary,

122
10:13.490 --> 10:15.980
add a set of square brackets,

123
10:15.980 --> 10:22.070
and then inside the square brackets we type the key word for word.

124
10:22.070 --> 10:30.260
So in this case, if we want to multiply our multiply() function is stored under the key which is this asterix.

125
10:30.260 --> 10:40.660
So now if I go ahead and just print this value as it is, you'll see that I get a function called multiply().

126
10:40.870 --> 10:43.660
So that doesn't give me any results.

127
10:44.260 --> 10:55.900
But if this part is equivalent to this, then that means this is equivalent to this function.

128
10:55.900 --> 11:02.480
So now if I want to use it I just need to add the parentheses straight afterwards.

129
11:02.480 --> 11:04.640
And you can see my PyCharm hints

130
11:04.640 --> 11:06.320
already knows what I'm trying to do.

131
11:06.320 --> 11:09.320
And it's asking me for n1 and n2.

132
11:09.320 --> 11:12.680
So I want to multiply four by eight.

133
11:12.680 --> 11:15.020
So I'll just type, (4, 8),

134
11:15.020 --> 11:20.900
and then now if I run this code you'll see that it's taken the 4 and the 8,

135
11:20.900 --> 11:30.840
and it has applied the correct operation, which is multiply, and put them inside this function to

136
11:30.840 --> 11:39.600
return the value 32, which then replaces this entire function call and then gets printed out into the

137
11:39.600 --> 11:40.440
console.

138
11:41.040 --> 11:46.690
So now that we figured out how to do this, the rest is up to you.

139
11:46.720 --> 11:53.710
So take a look at the bullet points for the functionality that we need for this calculator, and look

140
11:53.710 --> 11:57.190
at the demo software to see what you need to build.

141
11:57.190 --> 12:04.180
And then pause the video and using the starting file, complete the project by yourself.

142
12:04.180 --> 12:07.810
Pause the video and complete this project.

143
12:13.820 --> 12:18.050
Okay, let's walk through the solution together so that you can check your code,

144
12:18.050 --> 12:23.660
and also, if you get stuck, it'll be a good place to try to get unstuck.

145
12:23.780 --> 12:28.010
So I'm going to work through the functionality I've listed out one by one.

146
12:28.010 --> 12:34.710
That way, we can keep track of what's going on and make sure that we achieve all of the requirements

147
12:34.710 --> 12:36.030
of this project.

148
12:36.030 --> 12:40.470
So the first one is, "Program asks the user to type the first number."

149
12:40.470 --> 12:44.130
So that is going to be an input of some sort.

150
12:44.130 --> 12:49.980
And we're going to say, "What is the first number?"

151
12:50.460 --> 12:54.270
And then we add a colon add a space so that they can type it in.

152
12:54.270 --> 12:58.510
And we'll save this to a variable called num1.

153
12:58.690 --> 13:04.570
Now next the program is going to ask the user to type a mathematical operator.

154
13:04.570 --> 13:09.430
So plus, minus, multiply, or divide.

155
13:09.430 --> 13:12.580
So now that's going to be another input.

156
13:12.580 --> 13:17.860
And this is going to be, "Pick an operation."

157
13:17.860 --> 13:20.380
And then we're going to put a space.

158
13:20.620 --> 13:27.920
And now we're going to save this inside a variable which I'll call operation_symbol.

159
13:27.950 --> 13:31.160
Now how does a user know what to type.

160
13:31.160 --> 13:35.810
How do they know that they need to type one of these symbols.

161
13:35.810 --> 13:43.520
Well if we look at the demo you can see that we actually list out all four of the operators for them,

162
13:43.520 --> 13:47.220
and that way they'll know which symbol to use.

163
13:47.220 --> 13:49.020
So how can we do that?

164
13:49.020 --> 13:57.720
Well, we could use our existing dictionary called operations to give them an idea of what to type.

165
13:57.720 --> 14:06.150
So we could, for example, use what we've learned about looping through dictionaries, "for symbol in

166
14:06.150 --> 14:07.800
operations..."

167
14:07.800 --> 14:13.000
And then for each of the symbols, we know that when we loop through dictionaries, it just gives us

168
14:13.000 --> 14:17.260
the key as the binding to the first variable.

169
14:17.260 --> 14:20.560
So then we can simply just print out the symbol.

170
14:20.560 --> 14:26.710
So let's run this file as it is and see our first input trigger,

171
14:26.710 --> 14:34.000
and then we've got each of our keys in our operations dictionary being printed out.

172
14:34.000 --> 14:37.050
And finally we get to the input

173
14:37.050 --> 14:39.810
where it asks us for the operation_symbol.

174
14:40.800 --> 14:48.930
So now we're ready to tackle this sentence, which is, "Program asks the user to type the second number."

175
14:48.930 --> 14:53.010
So let's go ahead and save this to a variable called num2.

176
14:53.010 --> 15:00.550
And it's again going to be an input which is going to say, "What is the next number?"

177
15:03.370 --> 15:11.950
And then remember that with numbers such as num1 and num2, we need to actually use it as a number in

178
15:11.950 --> 15:14.920
order for our mathematical operations to work,

179
15:14.920 --> 15:22.570
because at the moment, if we try to do, for example, num1 + num2, it's going to treat them as

180
15:22.570 --> 15:23.150
strings.

181
15:23.150 --> 15:30.710
So if I try to add 3 to 3, it's going to give me 33 instead of 6.

182
15:30.710 --> 15:38.600
And so in order for that to not happen, we need to turn these numbers into either integers or into

183
15:38.600 --> 15:39.380
floats.

184
15:39.380 --> 15:45.920
Now because we've got division and we've got subtraction and we've got multiplication, in order for our

185
15:45.920 --> 15:50.910
calculators to be a little bit more mathematical and scientific,

186
15:51.300 --> 15:56.310
let's go ahead and turn each of these numbers into floats.

187
15:56.310 --> 16:03.150
So I'm going to wrap the input for num1 and num2 both inside floats.

188
16:04.080 --> 16:12.310
And now we're ready to tackle the next part, which is where the program is going to work out the result

189
16:12.310 --> 16:19.720
based on the chosen mathematical operator, and obviously work it out based on num1 and num2.

190
16:20.080 --> 16:25.000
So that is where the part comes in that we prepped for.

191
16:25.030 --> 16:33.760
We know that we can get hold of the name of these functions by tapping into the relevant key.

192
16:33.760 --> 16:40.070
And notice that we printed out each of these keys for the user so that they would pick it for their

193
16:40.070 --> 16:41.360
operation_symbol.

194
16:41.360 --> 16:49.790
So that means we can use this operation_symbol as the key selector to pick out the correct operation.

195
16:49.790 --> 16:55.400
And that's why we went through this whole process of setting up the dictionary and setting up the methods.

196
16:55.400 --> 17:05.040
So here if we go ahead and simply just print the operations, which is our dictionary, and then

197
17:05.040 --> 17:14.280
using the square brackets passing in the operation_symbol as the key, you'll see that if I type plus

198
17:14.280 --> 17:20.670
and it doesn't really matter which number you'll see, the function that gets picked will be add().

199
17:20.880 --> 17:30.580
But if I pick a different operation, say this one, then the function that gets selected will be multiply().

200
17:30.610 --> 17:36.460
And all we need to do to activate it is to add the parentheses.

201
17:36.460 --> 17:44.410
And then the two numbers that we're going to pass into any of these functions will be n1 and n2.

202
17:44.410 --> 17:55.940
So let's pass num1 for n1 and num2 for n2, so that these user-generated numbers gets placed into

203
17:55.940 --> 18:03.140
whichever function they desired, and then it will output the result of that calculation.

204
18:03.800 --> 18:10.670
So let's run this code and try something like again 3 + 3.

205
18:10.670 --> 18:15.240
And now we get 6, which is a lot better than before.

206
18:15.480 --> 18:23.880
Now notice that in the demo when we do the same thing, it gives us the whole calculation, which I

207
18:23.880 --> 18:28.260
think is quite nice because then the user sees the entire equation.

208
18:28.260 --> 18:32.100
So let's replicate that in our code as well.

209
18:32.100 --> 18:42.800
So instead of just printing the result let's go ahead and create an f-string and place this calculation

210
18:42.800 --> 18:44.870
inside a set of parentheses.

211
18:44.930 --> 18:53.720
Alternatively, you can place it inside a separate variable if you think this makes it easier to understand,

212
18:53.720 --> 18:55.250
and easier to read.

213
18:55.250 --> 18:59.000
And then we can simply place the answer into the f-string.

214
18:59.120 --> 19:08.880
So our equation is going to be num1 and then space, and then the operation_symbol and then space,

215
19:08.880 --> 19:13.380
and then num2 and then finally =,

216
19:13.800 --> 19:15.720
and we get to the answer.

217
19:15.720 --> 19:19.770
So let's try this again 3 + 3,

218
19:19.770 --> 19:27.370
we get this whole equation printed out which is kind of neat I think. So the next part that we need to

219
19:27.370 --> 19:34.960
tackle is the "Program should ask the user if they want to continue working with the previous result."

220
19:34.960 --> 19:42.910
So this answer keeps the result from the first calculation num1, whatever num2.

221
19:42.940 --> 19:50.090
Now if the user wants to continue working with that number, as is usual in most calculators,

222
19:50.090 --> 19:54.440
then we want to ask them that question right about now.

223
19:54.620 --> 20:05.750
So we're going to use an input and we're going to say, "Type 'y' to continue calculating with..."

224
20:05.750 --> 20:21.480
And then we'll insert the answer from the previous step and we'll say or type 'n' to start a new calculation.

225
20:22.200 --> 20:26.670
And that way we can get hold of their request.

226
20:26.670 --> 20:34.710
Do they want to restart and type in n1 again? Or do they want to simply just type an operation_symbol

227
20:34.710 --> 20:39.700
and a num2, and use the previous answer as num1?

228
20:39.850 --> 20:46.840
Now we need to do something about this input, so let's save it into something called choice.

229
20:46.840 --> 20:54.190
And we can save the choice, which we know is going to be 'y' or 'n', and we can check the choice.

230
20:54.190 --> 21:01.820
So, "if choice == ..." and we'll check first for 'y'.

231
21:02.480 --> 21:10.880
And then in that case, that means they want to continue working with the previous answer.

232
21:10.880 --> 21:16.370
And so what we'll do is we'll set num1 to equal the answer.

233
21:16.850 --> 21:27.240
And what we need now to happen is to get them to get the symbol and the next number and then print all

234
21:27.240 --> 21:27.900
of this out.

235
21:27.900 --> 21:29.970
So something like this.

236
21:29.970 --> 21:33.690
But as you notice this is kind of repetitive.

237
21:33.690 --> 21:36.870
So what do we do when things start repeating.

238
21:36.870 --> 21:44.910
We write a loop, or we use some sort of programming way to save us from typing out or having lots of

239
21:44.910 --> 21:46.140
repeated code.

240
21:46.140 --> 21:55.240
So we could have a variable called should_accumulate so that we could decide whether or not we should

241
21:55.240 --> 22:00.400
accumulate the answer to the next round of calculations.

242
22:00.400 --> 22:10.900
And while that is true, then we will perform all of this functionality and get to the end here.

243
22:10.900 --> 22:18.890
And if again, the answer is 'y', we set num1 to equal the answer from this step.

244
22:18.890 --> 22:24.020
And then because it's a loop, it's going to go all the way back to the top.

245
22:24.020 --> 22:29.240
So that means that this num1 here can't be inside the while loop,

246
22:29.240 --> 22:36.500
and so we need to take it outside so that it doesn't overwrite this part of the code here.

247
22:37.070 --> 22:39.100
So this part will loop.

248
22:39.100 --> 22:44.080
This part will start at the very beginning when the code starts.

249
22:44.080 --> 22:51.580
So now let's run our code file and see if all the functionality up to this point works as expected.

250
22:51.580 --> 22:55.360
So I'm going to add a 3 to 3.

251
22:55.600 --> 22:58.720
And I'm basically going to keep doing this.

252
22:58.720 --> 23:02.450
So I'm going to type 'y' to keep calculating.

253
23:02.450 --> 23:05.900
And again I'm going to add 3.

254
23:05.900 --> 23:08.720
So now you can see the next stage,

255
23:08.720 --> 23:12.350
they're taking 6 which is the answer from the previous stage,

256
23:12.350 --> 23:21.080
plus the new number that I wanted to add using the operation that I selected to calculate the new answer.

257
23:21.080 --> 23:23.360
So let's try a different operation.

258
23:23.360 --> 23:29.520
Let's say we've got 9, and I want to divide it by 2.

259
23:29.520 --> 23:34.590
And now I get 4.5, which is kind of nice because I've got floats now.

260
23:34.740 --> 23:41.520
So the next thing we need to tackle is what to happen when the user types 'n' at the moment, not what

261
23:41.520 --> 23:46.560
we want because we actually haven't got anything that addresses it.

262
23:46.560 --> 23:48.810
So let's write an else statement.

263
23:49.320 --> 23:53.860
And we know that we've got this variable that controls the while loop.

264
23:53.860 --> 23:57.550
So let's go ahead and set it to False,

265
23:57.550 --> 24:03.460
if the user no longer wants to accumulate the answer and continue calculating.

266
24:03.790 --> 24:12.160
Now at this stage, it's probably also a good idea to have some new lines so that it separates what

267
24:12.160 --> 24:14.170
the user was doing before.

268
24:14.180 --> 24:22.460
So this way we get that illusion, like what we see here when we type 'n', that we restart from the beginning.

269
24:22.820 --> 24:29.990
So once that's done, though, what we need to do is to start all the way from up here.

270
24:30.020 --> 24:32.180
So how can we do that?

271
24:32.210 --> 24:41.520
Well, what if we take all of this code and wrap it inside another while loop.

272
24:41.550 --> 24:48.960
Now, while that would work, it might be a little bit confusing, and there's actually a slightly simpler

273
24:48.960 --> 24:50.400
way of doing this.

274
24:50.400 --> 24:54.240
We could create a function called calculator().

275
24:54.240 --> 24:58.680
And this function is going to include all of this code.

276
24:58.680 --> 25:02.490
So I'm going to select all of it and then indent it in one go.

277
25:02.680 --> 25:10.210
Now what I'm going to do is I'm going to call this function inside itself,

278
25:10.210 --> 25:13.900
and this is something in programming known as recursion.

279
25:13.900 --> 25:20.110
So if you solved this project using another while loop, that is perfectly acceptable,

280
25:20.110 --> 25:22.240
and it's a perfectly good answer.

281
25:22.240 --> 25:26.500
But while we're here, another wild joke,

282
25:26.830 --> 25:29.900
I want to show you how you can also use recursion.

283
25:30.470 --> 25:36.620
So we've got this function called calculator(), which we obviously need to call outside the function

284
25:36.620 --> 25:40.850
so that it runs after the program starts.

285
25:40.850 --> 25:47.270
And then at this line it sees it, and then it goes into the definition and starts running all of these

286
25:47.270 --> 25:48.410
lines of code.

287
25:48.410 --> 25:54.810
But once it gets to here in the else statement, we're going to call calculator again.

288
25:54.810 --> 26:01.470
And what that's going to do is it's going to go to the very beginning of the calculator and try to run

289
26:01.470 --> 26:10.770
all of this code, and it will continue recursion until the user decides to choose something else.

290
26:10.770 --> 26:13.980
So let's try out this functionality.

291
26:13.980 --> 26:15.910
Our upgraded calculator.

292
26:15.910 --> 26:17.620
Let's try, 4 * 5 = 20

293
26:17.620 --> 26:22.270
.

294
26:22.270 --> 26:26.830
Let's type 'n' so that we start a new calculation.

295
26:26.830 --> 26:33.340
We've got all the space added between the previous calculation to appear as if we're starting from the

296
26:33.340 --> 26:34.030
beginning.

297
26:34.030 --> 26:37.270
And then we'll try and do a different calculation.

298
26:37.270 --> 26:40.230
What's 3 / 2?

299
26:40.500 --> 26:46.140
And now we could type 'y' to continue working with that 1.5,

300
26:46.140 --> 26:49.830
or we could type 'n' to start a new number.

301
26:49.830 --> 26:57.660
The only final thing we need to do is just to add the logo, so that when the calculator restarts up

302
26:57.660 --> 27:00.210
here, we actually see the logo.

303
27:00.210 --> 27:04.030
So let's first do our import right at the top.

304
27:04.030 --> 27:07.180
And the file is as always called art.

305
27:07.180 --> 27:14.680
And once we've got that we're going to make the print statement for the art.logo,

306
27:14.710 --> 27:18.820
the first thing that happens in our calculator program.

307
27:18.820 --> 27:20.980
So let's do a final rerun.

308
27:20.980 --> 27:25.870
And you can see we've got our calculator with our nice little logo.

309
27:25.870 --> 27:30.170
And then we got our calculation abilities.

310
27:31.820 --> 27:42.740
We can either accumulate to continue working with the previous numbers, or we can simply type 'no' to

311
27:42.740 --> 27:46.610
start calculating with a brand new number.

312
27:46.940 --> 27:48.290
So there you have it.

313
27:48.290 --> 27:52.440
That is the calculator program in its entirety.

314
27:52.440 --> 27:56.640
And if you want, you can always improve these projects.

315
27:56.640 --> 27:58.770
They are, as I say, always,

316
27:58.770 --> 27:59.970
they are learning projects.

317
27:59.970 --> 28:06.480
So they're trying to get you to use some piece of knowledge that you learnt during the day's lessons

318
28:06.480 --> 28:09.720
or previous lessons, but they're not perfect projects.

319
28:09.720 --> 28:13.920
And if you wish, you can always write more code and do more research.

320
28:13.920 --> 28:17.410
Search the internet to make it your very own version.

321
28:17.410 --> 28:23.350
But if you're satisfied with what you've learned from this lesson, and you've understood all of it

322
28:23.350 --> 28:29.650
enough to be able to build this project, or be able to understand the solution code and go back to

323
28:29.650 --> 28:35.830
build your project, then I'm happy that you've managed to get through today's content.

324
28:36.070 --> 28:40.900
So all that's left to do is rest up and I'll see you tomorrow.