1
00:00:00,150 --> 00:00:03,630
So we're now ready to tackle our final project of the day

2
00:00:03,900 --> 00:00:06,900
and that's building up a calculator program.

3
00:00:07,950 --> 00:00:12,150
If you head over to calculator-final.appbrewery.repl.run,

4
00:00:12,450 --> 00:00:14,610
you'll see how the final project works.

5
00:00:15,240 --> 00:00:20,240
We get to enter a number and then pick an operation and then pick the next

6
00:00:21,360 --> 00:00:25,500
number and it will calculate the result. Now,

7
00:00:25,800 --> 00:00:26,550
at this point,

8
00:00:26,550 --> 00:00:30,900
we can either continue to calculate with the result from the previous step.

9
00:00:30,960 --> 00:00:34,860
So let's multiply it by two, alternatively,

10
00:00:34,890 --> 00:00:39,240
I can type N and that clears the screen and lets me start out a new

11
00:00:39,240 --> 00:00:40,170
calculation,

12
00:00:42,990 --> 00:00:44,760
like so. Now,

13
00:00:44,790 --> 00:00:48,570
because a lot of the concepts that are covered in this project are actually

14
00:00:48,570 --> 00:00:49,440
quite difficult,

15
00:00:49,830 --> 00:00:53,850
I want to go through this project together and while we're building it up,

16
00:00:53,910 --> 00:00:58,910
there's also a lot of small challenges along the way that I want you to tackle.

17
00:00:59,700 --> 00:01:04,379
Once you're ready, head over to the calculator starting file and fork

18
00:01:04,379 --> 00:01:06,210
your own copy to get started.

19
00:01:06,380 --> 00:01:07,213
Right?

20
00:01:09,170 --> 00:01:13,520
This calculator is going to have some different functions that it can do.

21
00:01:13,670 --> 00:01:17,270
So for example, it will start out with a add function,

22
00:01:17,720 --> 00:01:20,960
which takes two inputs and n1 and n2,

23
00:01:21,440 --> 00:01:26,030
and then it's simply just returns n1 + n2.

24
00:01:26,900 --> 00:01:29,930
This is pretty simple. This is all that there is to

25
00:01:29,930 --> 00:01:33,920
the functionality of adding, takes one number and adds it to the other one.

26
00:01:34,550 --> 00:01:39,550
And I want you to go ahead to create the functions in the same format for these

27
00:01:41,360 --> 00:01:45,620
other mathematical operations. So subtract,

28
00:01:45,770 --> 00:01:50,720
multiply, and divide. Pause the video and complete this challenge.

29
00:01:51,860 --> 00:01:52,693
Right?

30
00:01:54,800 --> 00:01:58,430
All right. So subtract is as simple as add.

31
00:01:58,490 --> 00:02:02,420
So we'll just call it subtract. We pass in again

32
00:02:02,480 --> 00:02:04,550
two numbers, n1 and n2,

33
00:02:05,090 --> 00:02:09,530
and we'll return the outcome as n1 - n2.

34
00:02:11,330 --> 00:02:16,310
And for time sake, I'm just going to copy and paste these for the last two

35
00:02:16,610 --> 00:02:20,540
and I'm going to rename them multiply and divide.

36
00:02:20,900 --> 00:02:23,330
And then when numbers are multiplied,

37
00:02:23,360 --> 00:02:27,860
we use the asterisk sign and the number of divided we use the forward slash.

38
00:02:28,430 --> 00:02:33,380
So now we have four functions; add, subtract, multiply, and divide.

39
00:02:34,040 --> 00:02:39,040
And what we want to do is we want to somehow store these functions inside a

40
00:02:39,770 --> 00:02:43,130
dictionary. Here's another challenge for you.

41
00:02:43,520 --> 00:02:47,870
Can you create a dictionary where the keys are

42
00:02:47,960 --> 00:02:52,960
each of these symbols that we've used to add or subtract or multiply,

43
00:02:53,990 --> 00:02:57,200
and the values are just the names of the functions.

44
00:02:57,440 --> 00:03:00,100
So pause the video and try that now.

45
00:03:02,230 --> 00:03:06,760
Alright, let's go ahead and create this dictionary. Now we start out of course,

46
00:03:06,790 --> 00:03:10,390
with a set of curly braces and as I mentioned,

47
00:03:10,690 --> 00:03:15,690
each of the keys are going to be the symbols for the operations and the values

48
00:03:16,150 --> 00:03:20,140
are going to be the names of the functions. So we've got plus,

49
00:03:20,980 --> 00:03:24,820
and then we've got subtract, multiply,

50
00:03:24,910 --> 00:03:28,720
and divide. So now we have this dictionary,

51
00:03:28,750 --> 00:03:33,010
let's go ahead and save it inside a variable called operations.

52
00:03:33,700 --> 00:03:38,700
Now this dictionary is going to act as the means in which we're going to call

53
00:03:40,480 --> 00:03:43,870
these functions. At some point down here,

54
00:03:44,200 --> 00:03:49,200
we want to be able to tap into the operations and then pass in the key that we

55
00:03:49,510 --> 00:03:51,670
want, so for example multiply,

56
00:03:52,270 --> 00:03:56,620
and then store this as the function that we want.

57
00:03:57,340 --> 00:04:02,110
That means we can then call this function and then pass in one number and a

58
00:04:02,110 --> 00:04:02,950
second number

59
00:04:03,430 --> 00:04:07,480
and this function is currently going to act as the multiply function. Now,

60
00:04:07,480 --> 00:04:08,950
if we change this to plus,

61
00:04:09,220 --> 00:04:13,090
then this function is now going to act as the add function.

62
00:04:14,350 --> 00:04:17,800
So now the next thing we're going to do is we're going to create a variable

63
00:04:17,800 --> 00:04:18,820
called num1

64
00:04:19,690 --> 00:04:23,290
where we ask the user for an input to say

65
00:04:23,350 --> 00:04:24,880
what's the first number.

66
00:04:25,780 --> 00:04:30,160
And there's number of course has to be converted into an integer.

67
00:04:31,180 --> 00:04:35,230
Now, the next thing to do is to ask them for the second number,

68
00:04:35,260 --> 00:04:38,770
so this is going to be pretty much the same as this line of code,

69
00:04:39,340 --> 00:04:42,640
other than the word second. Now,

70
00:04:42,640 --> 00:04:47,200
the next thing is we want to ask them which of these operations they want to do.

71
00:04:47,800 --> 00:04:48,670
Firstly,

72
00:04:48,760 --> 00:04:53,760
we want to be able to loop through this dictionary and print out each of these

73
00:04:54,670 --> 00:04:55,503
keys.

74
00:04:55,840 --> 00:05:00,070
And then we're going to ask the user to type out one of them so that we can

75
00:05:00,070 --> 00:05:03,130
figure out which operation they actually want to do.

76
00:05:03,790 --> 00:05:06,130
Go ahead and try to use a for loop

77
00:05:06,340 --> 00:05:11,320
to loop through this dictionary of operations and print out each of these

78
00:05:11,590 --> 00:05:16,120
symbols. All right,

79
00:05:16,180 --> 00:05:17,860
now we're going to use a for loop

80
00:05:17,920 --> 00:05:20,890
and we know that when we use a four loop with a dictionary,

81
00:05:21,130 --> 00:05:26,130
it will loop through all the keys rather than the values or the actual entries.

82
00:05:27,010 --> 00:05:32,010
We can say for each symbol in the dictionary operations,

83
00:05:35,080 --> 00:05:39,970
go ahead and print out the symbol. And of the moment

84
00:05:39,970 --> 00:05:43,600
if I run this code, you'll see that it'll ask us for the first number,

85
00:05:43,930 --> 00:05:44,950
the second number,

86
00:05:45,250 --> 00:05:48,550
and then it's gonna loop through that list and then print out all the

87
00:05:48,550 --> 00:05:49,390
operations.

88
00:05:49,810 --> 00:05:54,810
So now we're ready to go ahead and ask them for a operation symbol. Pick and

89
00:05:56,200 --> 00:05:58,010
operation from the line above.

90
00:05:58,100 --> 00:06:03,100
So they're going to see all of these keys and then they get to pick an operation

91
00:06:03,530 --> 00:06:08,150
like so. And that symbol is now going to be saved within this variable.

92
00:06:08,840 --> 00:06:13,840
So now we can go ahead and take that operation symbol to pick out the value

93
00:06:16,040 --> 00:06:19,340
that's associated with it. For example, if in this case

94
00:06:19,400 --> 00:06:21,890
the user chose the plus symbol,

95
00:06:22,340 --> 00:06:24,950
then we're gonna pick out the add function.

96
00:06:25,580 --> 00:06:27,800
And then just as I shown you before,

97
00:06:27,860 --> 00:06:31,850
we want to use that add function to add these two numbers together.

98
00:06:32,360 --> 00:06:32,960
But of course,

99
00:06:32,960 --> 00:06:36,890
if they picked a different symbol then we want to be able to carry out the

100
00:06:36,950 --> 00:06:41,950
different operation. See if you can get this calculator to work so we can print out

101
00:06:43,850 --> 00:06:47,750
the num1 and then the operation symbol,

102
00:06:48,320 --> 00:06:53,180
and then num2, and finally the equal sign

103
00:06:53,570 --> 00:06:58,570
and then the answer. See if you can make this line of code work and make sure that

104
00:07:00,740 --> 00:07:04,700
it works whenever you choose any of these operations,

105
00:07:05,150 --> 00:07:08,150
and it will calculate it using these numbers that you input.

106
00:07:08,570 --> 00:07:09,470
Pause the video now.

107
00:07:12,530 --> 00:07:12,940
All right. 

108
00:07:12,940 --> 00:07:17,940
The first thing we need to do is to get hold of the calculation function

109
00:07:19,210 --> 00:07:22,390
and this is going to be done using the operation symbol.

110
00:07:22,780 --> 00:07:26,590
So we're going to tap into our dictionary of operations here,

111
00:07:27,070 --> 00:07:29,710
and then we're going to use the square brackets to pass in

112
00:07:30,040 --> 00:07:31,360
the operation symbol.

113
00:07:31,810 --> 00:07:36,810
Now we can use this calculation function as if it were any of these functions.

114
00:07:38,740 --> 00:07:41,650
And that's of course, based on the choice the user made here.

115
00:07:42,280 --> 00:07:47,280
So we can now say calculation function and we can pass in the num1 as the

116
00:07:47,830 --> 00:07:50,860
first input, num2 as the second input.

117
00:07:51,370 --> 00:07:55,060
And of course our function all have a return value.

118
00:07:55,480 --> 00:08:00,430
And so this part is going to be replaced when the code runs with the actual

119
00:08:00,430 --> 00:08:04,870
answer. So we can now save that inside a variable called answer,

120
00:08:05,290 --> 00:08:09,520
and then that will get sent over here and we can now test it out.

121
00:08:10,420 --> 00:08:13,360
So let's try adding three plus five.

122
00:08:13,870 --> 00:08:17,710
So I'm going to use the add symbol and we end up with three plus five equals

123
00:08:17,710 --> 00:08:21,010
eight. Now what about something a little bit more complicated?

124
00:08:21,190 --> 00:08:25,930
Uhm, 45, 78 and we'll choose multiply.

125
00:08:26,830 --> 00:08:30,790
Now, I think in reality that it actually makes more sense to put num2

126
00:08:31,150 --> 00:08:35,710
after the user's enter the operation symbol so that you actually end up with a

127
00:08:35,710 --> 00:08:40,240
better user experience like this. You can pick the first number,

128
00:08:40,450 --> 00:08:43,570
pick the operation, and then pick the second number.

