So we're now ready to tackle our final project of the day
and that's building up a calculator program.
If you head over to calculator-final.appbrewery.repl.run,
you'll see how the final project works.
We get to enter a number and then pick an operation and then pick the next
number and it will calculate the result. Now,
at this point,
we can either continue to calculate with the result from the previous step.
So let's multiply it by two, alternatively,
I can type N and that clears the screen and lets me start out a new
calculation,
like so. Now,
because a lot of the concepts that are covered in this project are actually
quite difficult,
I want to go through this project together and while we're building it up,
there's also a lot of small challenges along the way that I want you to tackle.
Once you're ready, head over to the calculator starting file and fork
your own copy to get started.
Right?
This calculator is going to have some different functions that it can do.
So for example, it will start out with a add function,
which takes two inputs and n1 and n2,
and then it's simply just returns n1 + n2.
This is pretty simple. This is all that there is to
the functionality of adding, takes one number and adds it to the other one.
And I want you to go ahead to create the functions in the same format for these
other mathematical operations. So subtract,
multiply, and divide. Pause the video and complete this challenge.
Right?
All right. So subtract is as simple as add.
So we'll just call it subtract. We pass in again
two numbers, n1 and n2,
and we'll return the outcome as n1 - n2.
And for time sake, I'm just going to copy and paste these for the last two
and I'm going to rename them multiply and divide.
And then when numbers are multiplied,
we use the asterisk sign and the number of divided we use the forward slash.
So now we have four functions; add, subtract, multiply, and divide.
And what we want to do is we want to somehow store these functions inside a
dictionary. Here's another challenge for you.
Can you create a dictionary where the keys are
each of these symbols that we've used to add or subtract or multiply,
and the values are just the names of the functions.
So pause the video and try that now.
Alright, let's go ahead and create this dictionary. Now we start out of course,
with a set of curly braces and as I mentioned,
each of the keys are going to be the symbols for the operations and the values
are going to be the names of the functions. So we've got plus,
and then we've got subtract, multiply,
and divide. So now we have this dictionary,
let's go ahead and save it inside a variable called operations.
Now this dictionary is going to act as the means in which we're going to call
these functions. At some point down here,
we want to be able to tap into the operations and then pass in the key that we
want, so for example multiply,
and then store this as the function that we want.
That means we can then call this function and then pass in one number and a
second number
and this function is currently going to act as the multiply function. Now,
if we change this to plus,
then this function is now going to act as the add function.
So now the next thing we're going to do is we're going to create a variable
called num1
where we ask the user for an input to say
what's the first number.
And there's number of course has to be converted into an integer.
Now, the next thing to do is to ask them for the second number,
so this is going to be pretty much the same as this line of code,
other than the word second. Now,
the next thing is we want to ask them which of these operations they want to do.
Firstly,
we want to be able to loop through this dictionary and print out each of these
keys.
And then we're going to ask the user to type out one of them so that we can
figure out which operation they actually want to do.
Go ahead and try to use a for loop
to loop through this dictionary of operations and print out each of these
symbols. All right,
now we're going to use a for loop
and we know that when we use a four loop with a dictionary,
it will loop through all the keys rather than the values or the actual entries.
We can say for each symbol in the dictionary operations,
go ahead and print out the symbol. And of the moment
if I run this code, you'll see that it'll ask us for the first number,
the second number,
and then it's gonna loop through that list and then print out all the
operations.
So now we're ready to go ahead and ask them for a operation symbol. Pick and
operation from the line above.
So they're going to see all of these keys and then they get to pick an operation
like so. And that symbol is now going to be saved within this variable.
So now we can go ahead and take that operation symbol to pick out the value
that's associated with it. For example, if in this case
the user chose the plus symbol,
then we're gonna pick out the add function.
And then just as I shown you before,
we want to use that add function to add these two numbers together.
But of course,
if they picked a different symbol then we want to be able to carry out the
different operation. See if you can get this calculator to work so we can print out
the num1 and then the operation symbol,
and then num2, and finally the equal sign
and then the answer. See if you can make this line of code work and make sure that
it works whenever you choose any of these operations,
and it will calculate it using these numbers that you input.
Pause the video now.
All right.
The first thing we need to do is to get hold of the calculation function
and this is going to be done using the operation symbol.
So we're going to tap into our dictionary of operations here,
and then we're going to use the square brackets to pass in
the operation symbol.
Now we can use this calculation function as if it were any of these functions.
And that's of course, based on the choice the user made here.
So we can now say calculation function and we can pass in the num1 as the
first input, num2 as the second input.
And of course our function all have a return value.
And so this part is going to be replaced when the code runs with the actual
answer. So we can now save that inside a variable called answer,
and then that will get sent over here and we can now test it out.
So let's try adding three plus five.
So I'm going to use the add symbol and we end up with three plus five equals
eight. Now what about something a little bit more complicated?
Uhm, 45, 78 and we'll choose multiply.
Now, I think in reality that it actually makes more sense to put num2
after the user's enter the operation symbol so that you actually end up with a
better user experience like this. You can pick the first number,
pick the operation, and then pick the second number.