Menu-driven calculator program: According to the input, it will perform addition, subtraction, multiplication, and division of two numbers.
Table of Contents
Calculator Program in Python using while loop
We will make a calculator by creating a menu-driven program in python for addition, subtraction, multiplication, and division of two numbers. The user will choose any options, and then we will take input from the user and call the appropriate function. We will perform some operations and print the result.
def addition(num1,num2): print("Addition=",num1+num2) def subtraction(num1,num2): print("Subtraction=",num1-num2) def multiplication(num1,num2): print("Multiplication=",num1*num2) def division(num1,num2): print("Division=",num1/num2) while True: print("1. Addition") print("2. Subtraction") print("3. Multiplication") print("4. Division") print("5. Exit") choice=int(input("Enter your choice(1-5):")) if choice==1: num1=int(input("Enter 1st Number:")) num2=int(input("Enter 2nd Number:")) addition(num1,num2) elif choice==2: num1=int(input("Enter 1st Number:")) num2=int(input("Enter 2nd Number:")) subtraction(num1,num2) elif choice==3: num1=int(input("Enter 1st Number:")) num2=int(input("Enter 2nd Number:")) multiplication(num1,num2) elif choice==4: num1=int(input("Enter 1st Number:")) num2=int(input("Enter 2nd Number:")) if num2 == 0: print('Infinity') else: division(num1,num2) elif choice==5: break else: print("Wrong Choice")
Output:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit Enter your choice(1-5):1
Enter 1st Number:2
Enter 2nd Number:6
Addition= 8
Video Solution
Quick algorithm for Calculator Program:
- Print the menu for those arithmetic operations our program can perform, i.e., addition, subtraction, multiplication, and division.
- Take the input from the user.
- According to their choice, call the appropriate function.
- Handle edge case for division operation, i.e., denominator can't be equal to zero.
- We will perform the arithmetic operation and print the result.
For example:
if choice==1: num1=int(input("Enter 1st Number:")) num2=int(input("Enter 2nd Number:")) addition(num1,num2)
Python Program for Calculator using functions
In this program, I made a calculator using functions, a dictionary and a while loop.
- functions: To perform different arithmetic operations like addition, subtraction, multiplication and division.
- dictionary: To assign every arithmetic symbol to the function name. ['+' to add function]
- while loop: To perform repetitive calculations according to user choice.
- Users can also continue calculations with the previous answers or can also perform the new calculation.
logo = """ _____________________ | _________________ | | | | | .----------------. .----------------. .----------------. .----------------. | |_________________| | | .--------------. || .--------------. || .--------------. || .--------------. | | ___ ___ ___ ___ | | | ______ | || | __ | || | _____ | || | ______ | | | | 7 | 8 | 9 | | + | | | | .' ___ | | || | / \ | || | |_ _| | || | .' ___ | | | | |___|___|___| |___| | | | / .' \_| | || | / /\ \ | || | | | | || | / .' \_| | | | | 4 | 5 | 6 | | - | | | | | | | || | / ____ \ | || | | | _ | || | | | | | | |___|___|___| |___| | | | \ `.___.'\ | || | _/ / \ \_ | || | _| |__/ | | || | \ `.___.'\ | | | | 1 | 2 | 3 | | x | | | | `._____.' | || ||____| |____|| || | |________| | || | `._____.' | | | |___|___|___| |___| | | | | || | | || | | || | | | | | . | 0 | = | | / | | | '--------------' || '--------------' || '--------------' || '--------------' | | |___|___|___| |___| | '----------------' '----------------' '----------------' '----------------' |_____________________| """ def add(n1,n2): return n1+n2 def subtract(n1,n2): return n1-n2 def multiply(n1,n2): return n1*n2 def divide(n1,n2): return n1/n2 operations={ '+':add, '-':subtract, '*':multiply, '/':divide } def calculator(): print(logo) choice=True num1=float(input("What's the first number?")) for key in operations: print(key) while choice: operation_symbol=input("Pick an operation: ") num2=float(input("What's the second number?")) ans=operations[operation_symbol](num1,num2) print(f"{num1} {operation_symbol} {num2} = {ans}") cont=input(f"Type'y' to continue calculating with {ans},or type 'n' to exit.: ").lower() if cont!='y': choice=False calculator() else: num1=ans calculator()
About this Menu-driven Calculator Program:
We made a simple calculator program in python using a while loop, if-else statements, and functions. According to the user's choice, this program can add, subtract, multiply and divide two numbers. It is also a menu-driven program to perform arithmetic operations in python.