Today, We will see how to write python code for simple calculator. We can create simple calculator using different functions. We will create menu driven program to code for simple calculator.

Lets see how to code for simple calculator

The program is divided into several parts:

Function definitions: The program starts with four functions defined for performing basic arithmetic operations such as addition ,subtraction,multiplication and division. These functions take two parameter x and y which are the two numbers to be operated on and returns the result of the operation on those two arguments.

Arithmetic operations
  • Menu options : After defining the functions, the program prints the list of menu options for the user to choose from, including “addition”,”subtraction”,”multiplication”,”division”. This options can be printed using print function. The user can select one of the options by entering the corresponding numbers(1,2,3,4).
Menu option list
  • User input : The program prompts the user to enter two numbers that will be used for the selected operation.
  • Calculation : Based on the users choice, the program calls the appropriate function and passes the two user input numbers as arguments to the function. The result of the opearation is then printed on the screen.
  • Invalid input handling : if the user enters an invalid choice, such as a non numeric input or a number that is not 1,2,3 or 4, the program will print an error message.

Here is the complete code for simple calculator using functions

# A simple calculator program in python

# Function to add two number
def add(x,y):
    return x+y

# Function to substract two number
def subtract(x,y):
    return x-y

# Function to multiply two number
def multiply(x,y):
    return x*y

# Function to divide two number
def divide(x,y):
    return x/y

# Print menu options
print("Select OPeration :")
print("1.Addition :")
print("2.Subtraction :")
print("3.Multiplication :")
print("4.Division :")

#Take input from user
choice = input(" Enter your choice(1/2/3/4) :")
num1 = int(input("Enter First number :"))
num2 = int(input("Enter Second number :"))

#Perfrom calculation based on user choice
if choice =='1':
    print(num1, "+", num2, "=", add(num1,num2))
elif choice =='2':
    print(num1, "-", num2, "=", subtract(num1,num2))    
elif choice =='3':
    print(num1, "*", num2, "=", multiply(num1,num2))
elif choice =='4':
    print(num1, "/", num2, "=", divide(num1,num2))

    print("Invalid input")



So, when you run this code, you will be prompted toenter two numbers and then you will see a menu of operators. After you choose an operation, the result will be displayed as below.

Output :

Python code for simple calculator using nested if statements

Lets break down the code step by step

  1. First we define a function called ‘calculator’ that takes three arguments ‘num1′,’num2′,’operator’. This function will perform the actual calculation and return result.
  2. Inside the ‘calculator’ function ,we use series of ‘if’ statements to check which operation the user wants to perform. If operation is ‘+’,we add the two numbers together using ‘+’ operator. If the operator is ‘-‘,we subtract ‘num2’ from ‘num1’. If the operator is ‘*’,we multiply ‘num1’ by ‘num2’. and if the operator is ‘/’ ,we divide ‘num1’ by ‘num2’.
  3. If the user enters an operator that is not one of these four, we return the string “invalid operator”.
  4. We then prompt the user to enter the first number and store their input as a float in variable ‘num1’ and ask user to enter second number and store the number in variable ‘num2’
  5. We get the user choice of operation using input () function and store it in the variable ‘operator’.
  6. Depending on the users choice,we call the calculator function and display result using print() function. If the user enters an invalid operator, we print error message.

Output of above code


Leave a Reply

Your email address will not be published.