In the last lesson we made it possible to reuse the result of the first
calculation.
The answer for the first calculation became our input for the next calculation,
allowing us to chain the calculations together. However,
this created quite a bit of repetition in our code
and it limits our user to just two calculations.
Let's allow the user to chain as many calculations as they want to.
And I want to throw this over to you as a challenge,
so can you use a while loop and the input function to achieve this?
Ask the user
this question, type 'y' to continue calculating with the previous result or type 'n'
to exit.
If the user types y then your calculator should allow them to continue chaining
together calculations with the previous answer.
If they type anything else the program should exit for now.
I'll give you a few seconds to pause the video before I show you the result.
All right. So the easiest way to solve this is through the use of a while loop.
So let's say that after we've asked for the first number and we've shown them
all of the different symbols that they could possibly pick from,
lets set a flag called should_continue.
And we'll set that to start out as true.
Then we can create a while loop and while it should continue as true,
then we're going to perform these things.
Now we want to change some of this text so that it's a little bit more reusable.
Let's say pick an operation,
and what's the next number instead of the second number.
So now we have a first number, we have a operation,
we have a second number and then we pick out the function from that dictionary
of operations and we get our answer.
So I'm going to change this back to answer and we're passing in num1
and num2 into the calculation function. Now,
once this line has been printed,
everything that occurs afterwards is pretty much repetition.
And instead we're going to ask the user to type y-- type y to continue calculating
with the answer from the previous step.
Let's make that an fstring and let's check what this is actually equal to.
So remember that the input function is also a function that has an output,
which is a little bit then zen,
but the output of this function is whatever the user typed in.
So if they typed in y then this part is going to become y. That's where we're
going to check.
So we're going to check if the result of the input from the user is equal to y,
then in that case that means we should continue and it should repeat back.
But when it loops back,
we want to make sure that num1 is equal to the answer from the previous step.
So we can say if this is true,
then num1 is going to be set to equal the answer.
But on the other hand, else, if they typed anything else,
so we could say maybe type y to continue calculating with answer or type n to
exit.
So now if they type y then the answer is going to be set as the num1 back at
the start of this while loop
so that this num1 becomes the answer from the previous step.
But if they typed n then we want this while loop to the end.
So we're going to change the flag should_continue to false.
So now let's give that a run.
Right?
Let's say the first number is five.
Then we're going to add three to five and then we're going to type y to
continue. So now we get to pick another operation,
so let's multiply, um,
eight by two and we get the result from the next step and we can actually keep
going until we're basically done, right?
So we can take the 16 and we can divide it by four.
But if we type 'n' on the other hand,
then our program ends and we see the prompt once more.
And that's the solution to the challenge,
but it's a little bit sad to just exit, right?
Because with a calculator more often than not,
what you want to do is to start a fresh calculation where you get to determine
the first number and the second number once more.
If the user didn't want to exit
but if they wanted to start a new calculation,
then how can we get them to go back all the way up here so that they provide the
num1 as a fresh input? This is a little bit tricky
and in programming, this concept is known as recursion.
It's basically the idea that you could have a function that calls itself.
So let's define a new function called calculator and this function takes no
inputs and has no outputs.
But all of this code that we've got here so far is inside the calculator
function. To begin when we start,
we have to call the calculator function in order for it to find the place where
this function was defined and to actually carry out all of these instructions.
Now,
the next thing we get to do is a little bit interesting because when the user
types no,
and says that they don't want to continue calculating with the previous answer
but instead they want to start a new calculation,
what we want to do instead of just exiting the while loop,
we want to call the calculator function because what this is going to do is it's
basically going to take us all the way back up to the beginning where we get to
enter a new input again.
And remember that once you reach the end of a function,
everything gets reset to the beginning. So should_continue becomes true again
and this while loop will continue working.
So let's try running this new version.
We've performed a calculation and now we want to start a new calculation,
so I'm going to type n. And now I get to again define the first number again.
This recursion basically happens because we're calling this calculator function
within the calculator function.
Essentially the code runs and runs and runs until it reaches here
and if these conditions are met and this calculator function is called,
then it goes and finds the calculator function in order to call it once more.
But now you have to be quite careful with while loops and with these recursive
functions
because let's say that instead of having all of this code and some checking code
to determine when I should call calculator,
if I actually just called calculator within calculator,
then this is going to be a infinite loop.
It's just going to keep going back and forth.
The calculator function calls the calculator function,
which goes back up and calls and it goes around and round around until forever.
Again,
be careful and make sure that there is some sort of condition that needs to be
met in order for this function to call itself. In the next lesson,
we're gonna add the finishing touches to our program and also fix a bug that you
may have already spotted at this point.