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 the link for the final demo project, you can see how it works.
So firstly, it asks us for the first number that we want to calculate with,
so let's start with 5.
And then it asks us to type to pick an operation + - * /.
So let's pick a multiply by typing the asterisk sign.
Now it asks for the next number.
So let's say we want to multiply 5 by 3,
so we'll type 3.
And then once I hit Enter it will perform the operation using floating point numbers,
and then it will give us 15.0.
So 3 * 5 is 15.0,
but now it asks us to type 'y' to continue calculating with the previous result, 15.0, or type 'n' to start a new
calculation.
So let's try the first version
first. Let's type y,
and now it asks us again to pick an operation.
So notice how this is repeated,
the same as here,
the Step 2 here.
So now I'm going to pick a different operation let's say add.
And now I'm working with 15.
So 15 plus, let's say 15 + 5,
and let's hit Enter.
And now it calculates our final result which is 15 + 5 = 20.0.
So notice how in this version it took the result of the previous calculation as the first number instead
of asking us for it.
And then we picked an operation, and then we added the second number and it performed the calculation.
So now we can continue working with the previous result,
or we can type 'n' to start a new calculation.
And again we loop back to the beginning, and we can start typing our first number, our second number,
et cetera.
So that's the goal for the final project.
But before you do that, there's something I want to show that you can do with functions.
So here I've created a function with the name add().
It has two inputs n1 and n2 and it outputs or returns n1 + n2.
So you'll notice this is one of the pieces of functionality that's going to be used in our calculator,
how to pass in two numbers to perform the addition functionality and then output the result.
Now we've seen functions declared like this before, but what you might not have seen is the ability
to store a function as a value of a variable.
So what does it mean?
Well, let's say I decided that adding numbers was my_favourite_operation.
Now this is a variable name and you've seen us create variables like this lots of times, right?
I could say my_favourite_operation = 3, or my_favourite_operation = "Hello", I can add strings,
I can add various values to the right-hand side of the equal sign to add a value to a named variable.
Now, what you might not have seen is you can also do this with functions.
However, if we want to store a function as a value, we can't add the parentheses because as I mentioned
before, we trigger a function with the parentheses.
The parentheses are also obviously for adding in inputs, but if we just want to store a function to
another name, give it another reference, then we would do it like this without the parentheses, only
with the name of the function.
So now what I can do is because that add() function is now stored under a new reference, my_favourite_operation
,
I can now trigger that function as if I was using the add() function.
So 2 and then comma 3.
And if I go ahead and print out the output of this result you'll see I get 5.
So this is pretty neat,
and it's going to be pretty important in order to create our calculator.
So now I want you to perform the first challenge.
And I'm going to go through the result of the first challenge,
so we set you up correctly for tackling this project.
So the goal is to first I want you to do this; I want you to write out the other three functions,
it's going to be subtract(), multiply() and divide().
So based on what you know about functions with inputs and outputs, and also obviously these three mathematical
operations, create the other three functions: subtract() multiply() and divide().
Pause the video now.
Okay, let's go through this because this should be relatively easy to do.
So let's first create subtract.
And we again are going to use n1 and n2.
And in this case we're going to return n1.
The first number subtract the second number, so n1 - n2.
And then we're going to create multiply again n1, n2,
and we're going to return n1 multiply,
so asterix, n2. And finally we're going to create divide,
and we're going to write n1 and n2,
and we're going to return n1 divide, which is the forward slash, n2 (n1/n2).
So there we have it.
We've got the other three functions created.
Now PyCharm is screaming at me because I don't have enough spaces between the functions,
but I'm going to write a lot more code,
so I really want it to be compact.
So we're just going to ignore that for now.
Now the next challenge I have for you is to add these four functions into a dictionary as the values,
so the dictionary will have,
the keys of this,
+ is one key, - is another key,
*, and finally, /.
So these are the four keys.
So pause the video and see if you can figure out, remembering what you learned about dictionaries in
previous lessons, how to store each of these functions as a value in a dictionary with these four keys,
and obviously, storing each function under the corresponding key.
Pause the video and see if you can complete this challenge.
Okay, let's go through the solution for this.
Let's say I decide to create a dictionary called operations,
and I'm going to save this as a dictionary.
So I open up my curly braces to signify that I'm creating a dictionary.
Now I mentioned that there are going to be four keys, right?
So "+", "-", "*", and "/".
Now relative to each of these keys I'm going to add the corresponding values,
and I do that using the colon symbol.
So the value for the key, "+" is going to be the function add.
And then I add a comma.
And I can do this for all four of my functions.
Again remembering not to trigger the functions because we're storing it and we're not using it,
so we don't want the parentheses.
So let me finish that for the rest of them.
And finally for divide.
So now we've got all four of these functions for our calculator, stored inside a dictionary called operations.
Now, just as we're able to trigger a function that's stored under a variable name, as you saw before,
we can do the same with these functions stored inside our dictionary.
So the final TODO that, I'm going to ask you to do before we let you go and do some independent work,
building this project is to use the dictionary operations to perform the calculations.
So I want you to try out multiply.
And I want you to multiply 4 by 8,
and to do this using the dictionary.
So see if you can figure out how to do this using what we've covered so far in today's lesson,
and also your knowledge of how to use dictionaries and how to access their values through their keys.
Pause the video if you need a hint, go back to the lesson on dictionaries and see if you can figure
it out by yourself without watching the solution.
Pause the video now.
Okay, so you might remember that the way that we access a value from a dictionary is we first tap into
the name of the dictionary,
add a set of square brackets,
and then inside the square brackets we type the key word for word.
So in this case, if we want to multiply our multiply() function is stored under the key which is this asterix.
So now if I go ahead and just print this value as it is, you'll see that I get a function called multiply().
So that doesn't give me any results.
But if this part is equivalent to this, then that means this is equivalent to this function.
So now if I want to use it I just need to add the parentheses straight afterwards.
And you can see my PyCharm hints
already knows what I'm trying to do.
And it's asking me for n1 and n2.
So I want to multiply four by eight.
So I'll just type, (4, 8),
and then now if I run this code you'll see that it's taken the 4 and the 8,
and it has applied the correct operation, which is multiply, and put them inside this function to
return the value 32, which then replaces this entire function call and then gets printed out into the
console.
So now that we figured out how to do this, the rest is up to you.
So take a look at the bullet points for the functionality that we need for this calculator, and look
at the demo software to see what you need to build.
And then pause the video and using the starting file, complete the project by yourself.
Pause the video and complete this project.
Okay, let's walk through the solution together so that you can check your code,
and also, if you get stuck, it'll be a good place to try to get unstuck.
So I'm going to work through the functionality I've listed out one by one.
That way, we can keep track of what's going on and make sure that we achieve all of the requirements
of this project.
So the first one is, "Program asks the user to type the first number."
So that is going to be an input of some sort.
And we're going to say, "What is the first number?"
And then we add a colon add a space so that they can type it in.
And we'll save this to a variable called num1.
Now next the program is going to ask the user to type a mathematical operator.
So plus, minus, multiply, or divide.
So now that's going to be another input.
And this is going to be, "Pick an operation."
And then we're going to put a space.
And now we're going to save this inside a variable which I'll call operation_symbol.
Now how does a user know what to type.
How do they know that they need to type one of these symbols.
Well if we look at the demo you can see that we actually list out all four of the operators for them,
and that way they'll know which symbol to use.
So how can we do that?
Well, we could use our existing dictionary called operations to give them an idea of what to type.
So we could, for example, use what we've learned about looping through dictionaries, "for symbol in
operations..."
And then for each of the symbols, we know that when we loop through dictionaries, it just gives us
the key as the binding to the first variable.
So then we can simply just print out the symbol.
So let's run this file as it is and see our first input trigger,
and then we've got each of our keys in our operations dictionary being printed out.
And finally we get to the input
where it asks us for the operation_symbol.
So now we're ready to tackle this sentence, which is, "Program asks the user to type the second number."
So let's go ahead and save this to a variable called num2.
And it's again going to be an input which is going to say, "What is the next number?"
And then remember that with numbers such as num1 and num2, we need to actually use it as a number in
order for our mathematical operations to work,
because at the moment, if we try to do, for example, num1 + num2, it's going to treat them as
strings.
So if I try to add 3 to 3, it's going to give me 33 instead of 6.
And so in order for that to not happen, we need to turn these numbers into either integers or into
floats.
Now because we've got division and we've got subtraction and we've got multiplication, in order for our
calculators to be a little bit more mathematical and scientific,
let's go ahead and turn each of these numbers into floats.
So I'm going to wrap the input for num1 and num2 both inside floats.
And now we're ready to tackle the next part, which is where the program is going to work out the result
based on the chosen mathematical operator, and obviously work it out based on num1 and num2.
So that is where the part comes in that we prepped for.
We know that we can get hold of the name of these functions by tapping into the relevant key.
And notice that we printed out each of these keys for the user so that they would pick it for their
operation_symbol.
So that means we can use this operation_symbol as the key selector to pick out the correct operation.
And that's why we went through this whole process of setting up the dictionary and setting up the methods.
So here if we go ahead and simply just print the operations, which is our dictionary, and then
using the square brackets passing in the operation_symbol as the key, you'll see that if I type plus
and it doesn't really matter which number you'll see, the function that gets picked will be add().
But if I pick a different operation, say this one, then the function that gets selected will be multiply().
And all we need to do to activate it is to add the parentheses.
And then the two numbers that we're going to pass into any of these functions will be n1 and n2.
So let's pass num1 for n1 and num2 for n2, so that these user-generated numbers gets placed into
whichever function they desired, and then it will output the result of that calculation.
So let's run this code and try something like again 3 + 3.
And now we get 6, which is a lot better than before.
Now notice that in the demo when we do the same thing, it gives us the whole calculation, which I
think is quite nice because then the user sees the entire equation.
So let's replicate that in our code as well.
So instead of just printing the result let's go ahead and create an f-string and place this calculation
inside a set of parentheses.
Alternatively, you can place it inside a separate variable if you think this makes it easier to understand,
and easier to read.
And then we can simply place the answer into the f-string.
So our equation is going to be num1 and then space, and then the operation_symbol and then space,
and then num2 and then finally =,
and we get to the answer.
So let's try this again 3 + 3,
we get this whole equation printed out which is kind of neat I think. So the next part that we need to
tackle is the "Program should ask the user if they want to continue working with the previous result."
So this answer keeps the result from the first calculation num1, whatever num2.
Now if the user wants to continue working with that number, as is usual in most calculators,
then we want to ask them that question right about now.
So we're going to use an input and we're going to say, "Type 'y' to continue calculating with..."
And then we'll insert the answer from the previous step and we'll say or type 'n' to start a new calculation.
And that way we can get hold of their request.
Do they want to restart and type in n1 again? Or do they want to simply just type an operation_symbol
and a num2, and use the previous answer as num1?
Now we need to do something about this input, so let's save it into something called choice.
And we can save the choice, which we know is going to be 'y' or 'n', and we can check the choice.
So, "if choice == ..." and we'll check first for 'y'.
And then in that case, that means they want to continue working with the previous answer.
And so what we'll do is we'll set num1 to equal the answer.
And what we need now to happen is to get them to get the symbol and the next number and then print all
of this out.
So something like this.
But as you notice this is kind of repetitive.
So what do we do when things start repeating.
We write a loop, or we use some sort of programming way to save us from typing out or having lots of
repeated code.
So we could have a variable called should_accumulate so that we could decide whether or not we should
accumulate the answer to the next round of calculations.
And while that is true, then we will perform all of this functionality and get to the end here.
And if again, the answer is 'y', we set num1 to equal the answer from this step.
And then because it's a loop, it's going to go all the way back to the top.
So that means that this num1 here can't be inside the while loop,
and so we need to take it outside so that it doesn't overwrite this part of the code here.
So this part will loop.
This part will start at the very beginning when the code starts.
So now let's run our code file and see if all the functionality up to this point works as expected.
So I'm going to add a 3 to 3.
And I'm basically going to keep doing this.
So I'm going to type 'y' to keep calculating.
And again I'm going to add 3.
So now you can see the next stage,
they're taking 6 which is the answer from the previous stage,
plus the new number that I wanted to add using the operation that I selected to calculate the new answer.
So let's try a different operation.
Let's say we've got 9, and I want to divide it by 2.
And now I get 4.5, which is kind of nice because I've got floats now.
So the next thing we need to tackle is what to happen when the user types 'n' at the moment, not what
we want because we actually haven't got anything that addresses it.
So let's write an else statement.
And we know that we've got this variable that controls the while loop.
So let's go ahead and set it to False,
if the user no longer wants to accumulate the answer and continue calculating.
Now at this stage, it's probably also a good idea to have some new lines so that it separates what
the user was doing before.
So this way we get that illusion, like what we see here when we type 'n', that we restart from the beginning.
So once that's done, though, what we need to do is to start all the way from up here.
So how can we do that?
Well, what if we take all of this code and wrap it inside another while loop.
Now, while that would work, it might be a little bit confusing, and there's actually a slightly simpler
way of doing this.
We could create a function called calculator().
And this function is going to include all of this code.
So I'm going to select all of it and then indent it in one go.
Now what I'm going to do is I'm going to call this function inside itself,
and this is something in programming known as recursion.
So if you solved this project using another while loop, that is perfectly acceptable,
and it's a perfectly good answer.
But while we're here, another wild joke,
I want to show you how you can also use recursion.
So we've got this function called calculator(), which we obviously need to call outside the function
so that it runs after the program starts.
And then at this line it sees it, and then it goes into the definition and starts running all of these
lines of code.
But once it gets to here in the else statement, we're going to call calculator again.
And what that's going to do is it's going to go to the very beginning of the calculator and try to run
all of this code, and it will continue recursion until the user decides to choose something else.
So let's try out this functionality.
Our upgraded calculator.
Let's try, 4 * 5 = 20
.
Let's type 'n' so that we start a new calculation.
We've got all the space added between the previous calculation to appear as if we're starting from the
beginning.
And then we'll try and do a different calculation.
What's 3 / 2?
And now we could type 'y' to continue working with that 1.5,
or we could type 'n' to start a new number.
The only final thing we need to do is just to add the logo, so that when the calculator restarts up
here, we actually see the logo.
So let's first do our import right at the top.
And the file is as always called art.
And once we've got that we're going to make the print statement for the art.logo,
the first thing that happens in our calculator program.
So let's do a final rerun.
And you can see we've got our calculator with our nice little logo.
And then we got our calculation abilities.
We can either accumulate to continue working with the previous numbers, or we can simply type 'no' to
start calculating with a brand new number.
So there you have it.
That is the calculator program in its entirety.
And if you want, you can always improve these projects.
They are, as I say, always,
they are learning projects.
So they're trying to get you to use some piece of knowledge that you learnt during the day's lessons
or previous lessons, but they're not perfect projects.
And if you wish, you can always write more code and do more research.
Search the internet to make it your very own version.
But if you're satisfied with what you've learned from this lesson, and you've understood all of it
enough to be able to build this project, or be able to understand the solution code and go back to
build your project, then I'm happy that you've managed to get through today's content.
So all that's left to do is rest up and I'll see you tomorrow.