Calculator | Python

TRY IT HERE

Okay, so I can admit it… on the surface, a calculator is not the most exciting project to have ever been coded. But, it is a staple project, and for a good reason. I guess you could say by not trying my hand at making this, it would be an uncalculated risk.

Puns aside, this project not only introduces a recursive concept to my code, but also uses a dictionary that holds functions. Let’s take a look at what makes this calculator, well, calculates…

As is usual with my projects, I start with importing an ASCII logo to use as a header for the program. I then defined four functions, each for a different type of calculation that can be completed. Designing the code in this way enables the easy enhancement of adding additional mathematical operations as their own functions in the future. For example, to add the ability to return the remainder of a division operation, it would be as simple as adding a new function like so:

def remainder (n1, n2):

return n1 % n2

Next, I created a dictionary called ‘operation’ that holds key/value pairs to associate the key symbol with their respective function. To add that same remainder function, as in the example above, to the ‘operation’ dictionary, I would then add the following within the brackets:

“%”: remainder,

Next, I defined a function called calculator() which holds all of the following code:

When calculator() is called, the ASCII header is first printed, and then the user is prompted to enter their first number which is saved in “num_1”. At this point, the variable ‘calc_continue’ is set to the Boolean value True, which will be referenced later to pull the program out of its loop. Speaking of the loop, while ‘calc_continue’ is set to True, the user is prompted to enter the operational symbol (stored in “operation_symbol”) and the second number to use (stored in “num_2”).

A variable called ‘calculation_function’ is then set to the actual function that corresponds to the symbol from the user’s input. This is accomplished by using the ‘operation’ dictionary to assign the key value from the input. After this, another variable called ‘answer’ is assigned with the actual value returned from the said function.

After the calculation, the program prints the result and prompts the user if they would like to use the value calculated in another operation. If so, the variable ‘num_1’ is set to the previously calculated value and the user is prompted for the next operation symbol with the number to be used. The user can continue calculating against their previous value until they enter ‘n’ at the continue prompt.

If the user enters “n”, calculator() is called recursively and the program starts at the beginning prompting for two numbers and the operation to be used. You may also notice a calculator() function call all by its lonesome at the very end of the code. This is actually the first function call that starts the program and is located outside of the ‘while’ loop in order to do so.


By my calculation, you are a rock star for sticking with me through these projects.

You can view and download the source code for this project on my GitHub by clicking here. Thank you for your time!

Previous
Previous

Black Jack Game: Part 1 | Python

Next
Next

Blind Auction Tool | Python