Writing a simple calculator in python

python simple calculator

We are going to create only a simple calculator that can perform addition, subtraction, multiplication and division. You can also return the result from the functions and print it out inside the caller class.

Python calculator function

To make calculator in python, first provide 5 options to the user, the fifth option for exit. The infinite loop is a while loop. Using the if-elif-else conditions, we are checking the type of the user input. Addition" ; print "2. In this example, we are printing out the result inside each function. This is a multi-line message and we are using triple comma for that. Now let's first try for the addition task by choosing or typing 1 as input and then enter any two number say 1 and 2 and then press enter key to check it out: Now let's re-run the calculator program to this time check it out for subtraction of two numbers say 2 and 1 as shown in the following sample run: Again re-run the same program, and this time, provide 4 as input to check for division task and supply two numbers, that is first number as 20 and second numbers as 3 then press enter key as shown here in the below sample run: Here is the same program written and run on python shell. We are using one while loop to create this infinite loop. That means a user can use the calculator as many time as he wants. We are going to create only a simple calculator that can perform addition, subtraction, multiplication and division. The program will first ask the user to enter the calculation type.

All of these functions take two numbers as parameter. Now let's first try for the addition task by choosing or typing 1 as input and then enter any two number say 1 and 2 and then press enter key to check it out: Now let's re-run the calculator program to this time check it out for subtraction of two numbers say 2 and 1 as shown in the following sample run: Again re-run the same program, and this time, provide 4 as input to check for division task and supply two numbers, that is first number as 20 and second numbers as 3 then press enter key as shown here in the below sample run: Here is the same program written and run on python shell.

The loop will run an infinite amount of time until the user stops it. Addition" ; print "2. If the input is not valid, the program is printing one message asking the user to enter a valid input.

python calculator github

In this example, we are printing out the result inside each function. This is the first snapshot: Below is the second snapshot: Here is the third snapshot: Same Program in Other Languages You may also like to learn or practice the same program in other popular programming languages:.

Just add more functions for each type of calculations and add more if-elif conditions. Inside the loop, we are using multiple if-elif-else cases.

python calculator online

We are using one while loop to create this infinite loop. For each calculation add, subtract, multiply, divide we have different functions.

Rated 7/10 based on 95 review
Download
Python Program to Make Calculator