Python Calculator

by just_another_person in Circuits > Software

147 Views, 3 Favorites, 0 Comments

Python Calculator

Screenshot 2024-07-29 030342.png

In this Instructable you'll learn how to make a simple calculator using Python that can calculate functions with two numbers, including integers and decimals.

Throughout the tutorial I'll be explaining the code step by step, so its super beginner friendly and perfect for anyone looking for a quick (and useful) intro project to python!

If you're comfortable with the software, feel free to also skip to the bottom to the GitHub link :)

Supplies

Screenshot 2024-07-29 025036.png

Since we're just programming for this project, all you need is Python IDLE. You can download the latest version at their website! https://www.python.org/downloads/

Selecting the Operation

Screenshot 2024-07-29 031428.png

Lines 1-5-- Print statements:

These are the basic 'print' statements we use to set up our code. When you run the program, they'll show up to give you some context of what operations you can use. Feel free to modify!


Line 7-- the 'while True' loop:

The 'while True' loop is super useful because it allows everything inside it to be repeated indefinitely, or until there is a break command (to 'break' out of the loop). This will come in use if you want to make multiple calculations.


Lines 8-14-- Choosing the operation!

When figuring out which operation to use, the code relies on user feedback (represented by 'input'), which it stores as the variable 'operation'. If the variable is stored as either a, b, c, or d, then it goes on to ask for the first and second number for the calculation (lines 10-11). By adding 'float', it stores the numbers as floating-point numbers.

If the user input was not one of the options (i.e.-- you inputted 'f' instead of a, b, c, or d), then the else statement will be triggered (lines 13-14)

Actually Doing the Math

Screenshot 2024-07-29 032700.png

Now, the math part of the code is actually quite simple, as we've already done the work of storing our inputted numbers.

For each operation, you can make an 'if' statement that details what the computer should do for each of the operations selected (e.g.-- line 16). For each calculation, simply use the variables we stored in step 1 (x and y).

Then, store the result of the calculations in a variable (I used 'result'), and print it.

Further Calculations

Screenshot 2024-07-29 033327.png

Although you can end the code at the previous step and have a pretty good calculator, if you want to go a step further you can ask the user if they want to continue doing calculations or not. Based on their input, if they answer anything other than 'no', the while true loop continues to repeat. However, if they answer 'no', it breaks the loop, and the code ends. Of course, you can modify the commands and requirements for breaking the code.

Test

Screenshot 2024-07-29 033853.png

Woohoo yay you've made you're calculator! Test it out with different integers, decimals, ect. and modify to your liking!!

GitHub Code!!