0% found this document useful (0 votes)
37 views

Build A Calculator Python Program Using OOP

The document describes how to build a basic calculator program in Python using object-oriented programming. It includes classes for the four basic math operations - addition, subtraction, multiplication, and division. The program prompts the user to select an operation and then enter two numbers. It calls the appropriate method to perform the calculation and display the result.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Build A Calculator Python Program Using OOP

The document describes how to build a basic calculator program in Python using object-oriented programming. It includes classes for the four basic math operations - addition, subtraction, multiplication, and division. The program prompts the user to select an operation and then enter two numbers. It calls the appropriate method to perform the calculation and display the result.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Build a Calculator Python Program using OOP

Problem: Write a Python program to create a simple calculator i.e a calculator


with addition, subtraction, multiplication, and division functionality using
object-oriented programming (OOP).

The user to choose what functionality they want to execute through an input,
then the user will input the required variables and the respective function will
be called to compute the result.

Sample Output:

class Calculator:
def add(self, a, b):
return a+b

def subtract(self, a, b):


return a-b

def multiply(self, a, b):


return a*b

def divide(self, a, b):


return a/b
#create a calculator object
my_cl = Calculator()

while True:

print("1: Add")
print("2: Subtract")
print("3: Multiply")
print("4: Divide")
print("5: Exit")

ch = int(input("Select operation: "))

#Make sure the user have entered the valid choice


if ch in (1, 2, 3, 4, 5):

#first check whether user want to exit


if(ch == 5):
break

#If not then ask fo the input and call appropiate methods
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

if(ch == 1):
print(a, "+", b, "=", my_cl.add(a, b))
elif(ch == 2):
print(a, "-", b, "=", my_cl.subtract(a, b))
elif(ch == 3):
print(a, "*", b, "=", my_cl.multiply(a, b))
elif(ch == 4):
print(a, "/", b, "=", my_cl.divide(a, b))

else:
print("Invalid Input")
PROGRAMMING QUIZ 2 FINALS:

1. How to make a Calculator in PYTHON?


2. To create this Program, make sure that you have a PyCharm IDE installed in your
computer.
3. If you are new to python programming and you don’t know what would be the the Python
IDE to use, Here are a list of Best Python IDE for Windows, Linux, Mac OS that are
available.
4. Also, here is the  How to Download and Install Latest Version of Python on Windows.
5. Follow the steps on how to make a calculator in Python.
Check this link for your reference:
https://itsourcecode.com/free-projects/python-projects/program-for- calculator-in-python-
with-source-code/
6. Please submit this on or before October 28, 2022.
7. Please see image for Sample Output:

You might also like