Write A Python Script To Create A Class That Performs Basic Calculator Operations

 In this article let's learn how to create a basic Python calculator using the Python class. Let's use Python class to perform all the automatic operations  in and print the results.


Ask the user to enter the two numbers and the The Automatic operation has a choice and then evaluate the function.

 

Write A Python Script To Create A Class That Performs Basic Calculator Operations

 

 


Write A Python Script To Create A Class That Performs Basic Calculator Operations

Define a class named as calculator and using the init function initialize the  automatic operator by passing an input to the calculator object.

In the init function ask user to enter two  integer variables and then using the try and except block evaluate the automatic operation try catching the zero division error and print the appropriate message as shown below.

class calculator:

   def __init__(self, operator):
       x = input("Enter First Number : ")
       y = input("Enter Second Number : ")
       try:
           result = eval(x + operator + y)
       except ZeroDivisionError:
           print("Divide by zero not possible")
       else:
           print(f"{x} {operator} {y} is : {result}")




Also in the main  program define a class named get_choice().  This function will display a menu to the user to enter his choice addition subtraction multiplication and division are quit the program.   This function accepts the user input and returns it to the main program.

def get_choice():
   return int(input("\n1: Add \n2: Subtract \n3: Multiply \n4: Divide \n5: Quit \nEnter Choice : "))





 in the main program let's use do while to iterate until the user selects program to quit.   now take a if else block and ask the user to enter a choice by calling get_choice function call.

Check if the user choice is equal to 1 then take an instance of the calculator object by passing + operator,  if choice is 2 instantiate with - operator,  if choice is 3 then call the calculation instance with multiply operator,  if the choice is 4 then instantiate class object with division operator.  if the choice is 5 then exit the  while loop,  that exit the program.

choice = 0
while choice != 5:
   choice = get_choice()
   if choice == 1: calculator("+")
   elif choice == 2: calculator("-")
   elif choice == 3: calculator("*")
   elif choice == 4: calculator("/")





Complete python code
====================================

class calculator:

   def __init__(self, operator):
       x = input("Enter First Number : ")
       y = input("Enter Second Number : ")
       try:
           result = eval(x + operator + y)
       except ZeroDivisionError:
           print("Divide by zero not possible")
       else:
           print(f"{x} {operator} {y} is : {result}")


def get_choice():
   return int(input("\n1: Add \n2: Subtract \n3: Multiply \n4: Divide \n5: Quit \nEnter Choice : "))

choice = 0
while choice != 5:
   choice = get_choice()
   if choice == 1: calculator("+")
   elif choice == 2: calculator("-")
   elif choice == 3: calculator("*")
   elif choice == 4: calculator("/")


 
Output
==================
1: Add
2: Subtract
3: Multiply
4: Divide
5: Quit
Enter Choice : 1
Enter First Number : 5.5
Enter Second Number : 5
5.5 + 5 is : 10.5

1: Add
2: Subtract
3: Multiply
4: Divide
5: Quit
Enter Choice : 2
Enter First Number : 34.8
Enter Second Number : 8.0
34.8 - 8.0 is : 26.799999999999997

1: Add
2: Subtract
3: Multiply
4: Divide
5: Quit
Enter Choice : 3
Enter First Number : 4.9
Enter Second Number : 9
4.9 * 9 is : 44.1

1: Add
2: Subtract
3: Multiply
4: Divide
5: Quit
Enter Choice : 4
Enter First Number : 55
Enter Second Number : 0
Divide by zero not possible

1: Add
2: Subtract
3: Multiply
4: Divide
5: Quit
Enter Choice : 5




Conclusion
================
Execute the above Python program by performing all the arithmetic operations mentioned in the choice note down the results.


Comment down below  if you have any suggestions to optimize the above Python program.






Post a Comment

0 Comments