Now that we've pretty much completed all of the logic and functionality of this
calculator, it's time to add in the bells and the whistles,
including adding in the calculator logo and also checking for any bugs.
Adding the calculator logo is pretty simple.
You've done this loads of times before.
All we have to do is import it from the art module,
and then we're going to print it at the beginning of our calculator function.
So this way
when we get to the end and the user wants to start a fresh calculation,
we'll also show them a logo again. Now the next thing to notice,
this is a bit of a bug that you might've seen or not.
But what happens if we try to enter a number with a decimal point?
Let's say we want to calculate 4.5 multiplied by 2.
Our program has already crashed
as soon as I've entered 4.5 and hit enter. As a challenge,
can you diagnose what went wrong and fix this bug? I'll give you a few seconds
to pause the video before I give you the
solution.
Alright. Here's the solution.
The bug goes back to our lesson on datatypes.Notice how it's giving us an error
and it's pointing to this line
number 26. Line number 26 takes this input
which is a string and converts it into an integer. But of course,
a number with the decimal place shouldn't be converted into an integer.
We should actually hold it as a floating point number instead. This way
we're able to perform more accurate calculations.
So instead of just 4 times 5,
we can do 4.5 times 5.5, for example.
So instead of using int here, I'm going to change that to float
and I'm going to do the same here for num2. Now, when we hit run,
I can actually perform calculations using numbers with decimal places,
but I can also use numbers which are whole.
So let's multiply 2.5 by 2, the answer is 5,
and you can see that all the numbers are being reported to a greater level of
accuracy, meaning we have numbers after the decimal point.
And this is all because we converted that int to a float.
Now that there's a lot more features that you could add to your calculator.
You could add a square root capability, you could add exponent capability,
but the important thing here was really for you to see how functions that
provide outputs are really useful
when you come to a more complex program where you can take the output from one
function call and pass it into the next function
call as an input.
So this output can be reused in different parts of the code.
And this not only allows us to reduce repetition and also make our more reusable
and more modular,
but also it gives us the flexibility of performing more actions after a function
has completed. Now,
we're going to be using a lot of the concepts that we've learned here today in
tomorrow's capstone project. A lot of things I've explained,
including things like the recursion
where we're calling a function within its own definition or things like setting a
flag using a wall loop to continue
some piece of code execution is all going to come in really,
really handy tomorrow.
So make sure that if there's anything that you don't understand here
that you review it and you're comfortable with what the code is doing before you
proceed. That's all for today and I'll see you tomorrow.