Write a simple calculator program in Python 3
Simple Calculator Program in Python 3 :
In this tutorial, we will learn how to create a Calculator using python 3. The program will read the inputs from the user continuously and based on the user input, it will perform some calculations. We are going to create only a simple calculator that can perform addition, subtraction, multiplication and division. You can add any other mathematical operations to it if you want.
You can even add GUI to this program using python TKinter. I am not going to cover it in this tutorial, but the process will be the same.
Before going through the code, let me show you how it is going to work :
The algorithm we are using in this program :
1. We are using one infinite loop to get input from the user continuously. That means a user can use the calculator as many time as he wants. The loop will run an infinite amount of time until the user stops it. We are using one while loop to create this infinite loop.
2. The calculator will exit if the user will enter ‘q’ as an input or the infinite while loop will exit if the user types ‘q’ at the beginning of the loop.
3. For each calculation (add, subtract, multiply, divide) we have different functions. We will call these functions from the main program.
4. Using an ‘if-else’ condition, we are checking what type of calculation the user wants to do.
Python Program :
You can download this program from here
Sample Output :
1. As you can see in the above program, we have four different functions defined at the beginning of the program. The add function is for adding two numbers, the sub function is for subtraction, the mul function is for the multiplication of two numbers and the div function is for finding out the division.
All of these functions take two numbers as parameter. In this example, we are printing out the result inside each function. You can also return the result from the functions and print it out inside the caller class.
2. Before starting the program, we are printing out one message to the user that + is used for addition, – is for subtraction, * is for multiplication, / is for division and q is to quit the game.
This is a multi-line message and we are using triple comma for that.
3. The infinite loop is a while loop. ‘while True’ will start that loop for an infinite number of times.
4. Inside the loop, we are using multiple if-elif-else cases. The program will first ask the user to enter the calculation type. It will read it and save it in the user_input variable. For reading this value, we are using the ‘input()’ method.
5. Using the if-elif-else conditions, we are checking the type of the user input. Based on the input type, we are calling the required function defined above.
6. If the input is not valid, the program is printing one message asking the user to enter a valid input. If the input is ‘q’, it will exit.
Add more Actions to this calculator :
You can add more actions to this calculator program. Just add more functions for each type of calculations and add more if-elif conditions. Happy Coding 🙂