0% found this document useful (0 votes)
0 views6 pages

Python Project

The document describes the design and implementation of an advanced calculator using Object-Oriented Programming in Python. It outlines the functionalities such as various arithmetic operations, user input validation, and a menu-driven interface, all encapsulated within a class. The program emphasizes modularity and reusability, showcasing good software design practices.

Uploaded by

ANSHU S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views6 pages

Python Project

The document describes the design and implementation of an advanced calculator using Object-Oriented Programming in Python. It outlines the functionalities such as various arithmetic operations, user input validation, and a menu-driven interface, all encapsulated within a class. The program emphasizes modularity and reusability, showcasing good software design practices.

Uploaded by

ANSHU S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Advanced Calculator Using OOP in Python

Aim:
To design and implement an advanced calculator using Object-Oriented
Programming (OOP) in Python that performs a variety of arithmetic operations
such as addition, subtraction, multiplication, division, power, square root,
modulo, and floor division, while ensuring user input validation without using
try-except. The calculator should be menu-driven and structured using class and
object concepts for modularity and reusability.

Procedure:
 Define a class named AdvancedCalculator to encapsulate all calculator
functionality.
 Create a method display_menu() to show all available operations
(Addition, Subtraction, Multiplication, Division, Power, Square Root,
Modulo, Floor Division, Exit).
 Write an input validation method is_valid_number() that checks whether
the user input is a valid integer or float (including negative numbers)
without using exception handling.
 Create a method get_number(prompt) to take input from the user and
validate it using the previous method.
 Define a method perform_operation(choice) that:
 Takes the user's menu choice.
 Accepts input for one or two numbers as needed.
 Performs the selected operation.
 Handles edge cases like division by zero or square root of a
negative number.
 Create a loop in the run() method that:
 Continuously shows the menu.
 Accepts and processes the user's choice.
 Breaks the loop when the user selects "Exit".
 Create an object of the class and call the run() method to start the
calculator.
Source Code:
import math

class AdvancedCalculator:
def __init__(self):
self.running = True

def display_menu(self):
print("\n===== Advanced Calculator =====")
print("1. Addition (+)")
print("2. Subtraction (-)")
print("3. Multiplication (*)")
print("4. Division (/)")
print("5. Power (x^y)")
print("6. Square Root (√x)")
print("7. Modulo (%)")
print("8. Floor Division (//)")
print("9. Exit")

def is_valid_number(self, s):


# Check if input is a valid integer or float (including negative)
return s.replace('.', '', 1).isdigit() or (s.startswith('-') and s[1:].replace('.', '',
1).isdigit())

def get_number(self, prompt):


value = input(prompt)
if self.is_valid_number(value):
return float(value)
else:
print("Invalid input! Please enter a numeric value.")
return None

def perform_operation(self, choice):


if choice in ['1', '2', '3', '4', '5', '7', '8']:
num1 = self.get_number("Enter first number: ")
num2 = self.get_number("Enter second number: ")

if num1 is None or num2 is None:


return

if choice == '1':
print("Result:", num1 + num2)
elif choice == '2':
print("Result:", num1 - num2)
elif choice == '3':
print("Result:", num1 * num2)
elif choice == '4':
if num2 != 0:
print("Result:", num1 / num2)
else:
print("Error: Cannot divide by zero.")
elif choice == '5':
print("Result:", num1 ** num2)
elif choice == '7':
print("Result:", num1 % num2)
elif choice == '8':
if num2 != 0:
print("Result:", num1 // num2)
else:
print("Error: Cannot perform floor division by zero.")

elif choice == '6':


num = self.get_number("Enter a number: ")
if num is not None:
if num >= 0:
print("Result:", math.sqrt(num))
else:
print("Error: Cannot calculate square root of negative number.")

elif choice == '9':


print("Exiting Calculator. Thank you!")
self.running = False
else:
print("Invalid choice. Please select a valid option.")

def run(self):
while self.running:
self.display_menu()
user_choice = input("Enter your choice (1-9): ")
self.perform_operation(user_choice)

# Create object and run calculator


calc = AdvancedCalculator()
calc.run()

Sample Output:
===== Advanced Calculator =====
1. Addition (+)
2. Subtraction (-)
3. Multiplication (*)
4. Division (/)
5. Power (x^y)
6. Square Root (√x)
7. Modulo (%)
8. Floor Division (//)
9. Exit
Enter your choice (1-9): 1
Enter first number: 10
Enter second number: 25
Result: 35.0

Result:
The Advanced Calculator program using OOP in Python was successfully
developed. It:
 Accepts valid numeric inputs from users.
 Performs multiple arithmetic operations accurately.
 Demonstrates good software design practices through the use of class and
object, promoting modularity and reusability.

You might also like