The project aims to design a calculator (addition, multiplication, subtraction, division) using Python.
In this project, I declare four functions:
then I return the values as variables a and b.
# addition of two numbers def addition(a, b): return a + b # subtraction of two numbers def subtraction(a, b): return a - b # multiplication two numbers def multiplication(a, b): return a * b # divition of two numbers def divition(a, b): return a / b
Then I take a while loop and inside the while loop, I give the select option as 1,2,3,4 so that users can select as per their choice. Here I take the input from the users and return the calculated answer.
print("Select operation.") print("1.Addition") print("2.Subtraction") print("3.Multiplication") print("4.Divition") while True: # Take input from the user select = input("SELECT(1/2/3/4): ") # Check if choice is one of the four options if select in ('1', '2', '3', '4'): n1 = float(input("Enter the 1st number: ")) n2 = float(input("Enter the 2nd number: ")) if select == '1': print(n1, "+", n2, "=", addition(n1, n2)) elif select == '2': print(n1, "-", n2, "=", subtraction(n1, n2)) elif select == '3': print(n1, "*", n2, "=", multiplication(n1, n2)) elif select == '4': print(n1, "/", n2, "=", divition(n1, n2)) break else: print("Invalid Input")
After successfully running the code, the output is coming as follows:
Select operation. 1.Addition 2.Subtraction 3.Multiplication 4.Divition SELECT(1/2/3/4): 4 Enter the 1st number: 9 Enter the 2nd number: 3 9.0 / 3.0 = 3.0
Submitted by Sudipta Ghosh (Sudipta609)
Download packets of source code on Coders Packet