Write a Python Program to Make Calculator Using Function

Provided a menu to the user of arithmetic operations,  addition, subtraction multiplication and division the user has to choose an option and provide his details to the program.

Add, subtract , multiply and divide two numbers using the function.

 

 

Write a Python Program to Make Calculator Using Function

 


Write a Python Program to Make Calculator Using Function

Let's define a function, calculate and pass a string to it in the function. Let's take a variable result and use an eval() function to evaluate the arithmetic string.  after getting the result print the arithmetic string and the result Using the print function call.

def calculate(my_string):
   result = eval(my_string)
   print(f"\nResult of {my_string} is : {result}")





In another function ask the user to enter his choice by displaying the menu, convert the choice into integer and check if invalid choice entered exit the program.  Else return the the choice to the  calling function

def ask_user():
   choice = int(input("\nMenu\n1: Addition \n2: Subtraction \n3: Product \n4: Division \n5: Exit \n\nEnter Your Choice : "))
   if 0 < choice < 5:
       return choice
   exit(0)





Use the infinite while loop to iterate by calling the function,  asking for a choice and then asking the user for two numbers N1 and N2.

Use the if else statement to switch to addition, subtraction multiplication and division choices of the user At the same time call the calculate function by passing the arithmetic string.

while True:
   option = ask_user()
   n1 = int(input("Enter First Number : "))
   n2 = int(input("Enter Second Number : "))

   if option == 1: calculate(f"{n1} + {n2}")
   elif option == 2: calculate(f"{n1} - {n2}")
   elif option == 3: calculate(f"{n1} * {n2}")
   elif option == 4 and n2 != 0: calculate(f"{n1} / {n2}")
   elif option == 4 and n2 == 0: print("Division by Zero Not Possible")

Also check if the second number N2 is zero and the user  chooses for division operation then print the error message division by zero is not possible.






Final program
========================

def calculate(my_string):
   result = eval(my_string)
   print(f"\nResult of {my_string} is : {result}")

def ask_user():
   choice = int(input("\nMenu\n1: Addition \n2: Subtraction \n3: Product \n4: Division \n5: Exit \n\nEnter Your Choice : "))
   if 0 < choice < 5:
       return choice
   exit(0)

while True:
   option = ask_user()
   n1 = int(input("Enter First Number : "))
   n2 = int(input("Enter Second Number : "))

   if option == 1: calculate(f"{n1} + {n2}")
   elif option == 2: calculate(f"{n1} - {n2}")
   elif option == 3: calculate(f"{n1} * {n2}")
   elif option == 4 and n2 != 0: calculate(f"{n1} / {n2}")
   elif option == 4 and n2 == 0: print("Division by Zero Not Possible")




Output
=======================


Menu
1: Addition
2: Subtraction
3: Product
4: Division
5: Exit

Enter Your Choice : 1
Enter First Number : 12
Enter Second Number : 12

Result of 12 + 12 is : 24

Menu
1: Addition
2: Subtraction
3: Product
4: Division
5: Exit

Enter Your Choice : 5




Conclusion
=====================

Try running the program by yourself and provide the random N1 and N2 values as an input to the program and see the result.

Using your programming  knowledge can you still make this program smaller and optimized.



Post a Comment

0 Comments