Skip to content

Commit 6bab1dd

Browse files
authored
Update easyPythonpi.py
This Python module, easypythonpi.py, is designed to help with a variety of common calculations, including basic arithmetic operations, binary-to-decimal conversion, Fibonacci sequence generation, and shape area calculations. It is built with simplicity and ease of use in mind, making it ideal for both beginners and intermediate users. The module also includes custom exceptions to handle invalid inputs gracefully.
1 parent dbd94af commit 6bab1dd

File tree

1 file changed

+121
-5
lines changed

1 file changed

+121
-5
lines changed

easyPythonpi/easyPythonpi.py

Lines changed: 121 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,130 @@
1111

1212
# define exception for invalid Binary Strings
1313

14+
# class InvalidBinaryException(Exception):
15+
# pass
16+
17+
# class InvalidNumberFibException(Exception):
18+
# def __init__(self, n, message="n is not valid, must be greater than or equal to 1"):
19+
# self.n = n
20+
# self.message = message
21+
22+
# class InvalidMeasurementShapeException(Exception):
23+
# def __init__(self, message="invalid measurement, must be greater than or equal to 0"):
24+
# self.message = message
25+
26+
"""This Python module, easypythonpi.py, is designed to help with a variety of common calculations, including basic arithmetic operations, binary-to-decimal conversion, Fibonacci sequence generation, and shape area calculations. It is built with simplicity and ease of use in mind, making it ideal for both beginners and intermediate users. The module also includes custom exceptions to handle invalid inputs gracefully."""
27+
28+
29+
# Define exception for invalid binary strings
1430
class InvalidBinaryException(Exception):
31+
"""Raised when an invalid binary string is provided."""
1532
pass
1633

34+
# Define exception for invalid Fibonacci numbers
1735
class InvalidNumberFibException(Exception):
36+
"""Raised when the Fibonacci number is invalid."""
1837
def __init__(self, n, message="n is not valid, must be greater than or equal to 1"):
19-
self.n = n
20-
self.message = message
21-
38+
self.n = n
39+
self.message = message
40+
super().__init__(self.message)
41+
42+
# Define exception for invalid measurements for shapes
2243
class InvalidMeasurementShapeException(Exception):
23-
def __init__(self, message="invalid measurement, must be greater than or equal to 0"):
24-
self.message = message
44+
"""Raised when invalid measurement values are provided for shapes."""
45+
def __init__(self, message="Invalid measurement, must be greater than or equal to 0"):
46+
self.message = message
47+
super().__init__(self.message)
48+
49+
# Functions for basic calculations
50+
51+
def add(num1, num2):
52+
"""Add two numbers."""
53+
return num1 + num2
54+
55+
def subtract(num1, num2):
56+
"""Subtract two numbers."""
57+
return num1 - num2
58+
59+
def multiply(num1, num2):
60+
"""Multiply two numbers."""
61+
return num1 * num2
62+
63+
def divide(num1, num2):
64+
"""Divide two numbers with error handling for division by zero."""
65+
try:
66+
return num1 / num2
67+
except ZeroDivisionError:
68+
return "Error: Division by zero is not allowed."
69+
70+
# Binary string to decimal conversion
71+
def binary_to_decimal(binary_str):
72+
"""Convert binary string to decimal."""
73+
try:
74+
# Check if the binary string is valid
75+
if not set(binary_str).issubset({'0', '1'}):
76+
raise InvalidBinaryException(f"Invalid binary string: {binary_str}")
77+
return int(binary_str, 2)
78+
except InvalidBinaryException as e:
79+
return str(e)
80+
81+
# Fibonacci sequence generator
82+
def fibonacci(n):
83+
"""Generate Fibonacci sequence up to the nth number."""
84+
if n < 1:
85+
raise InvalidNumberFibException(n)
86+
87+
fib_sequence = [0, 1]
88+
for i in range(2, n):
89+
fib_sequence.append(fib_sequence[-1] + fib_sequence[-2])
90+
return fib_sequence[:n]
91+
92+
# Shape area calculators
93+
94+
def calculate_circle_area(radius):
95+
"""Calculate the area of a circle given the radius."""
96+
if radius < 0:
97+
raise InvalidMeasurementShapeException("Radius must be non-negative.")
98+
return 3.14159 * radius * radius
99+
100+
def calculate_rectangle_area(length, width):
101+
"""Calculate the area of a rectangle."""
102+
if length < 0 or width < 0:
103+
raise InvalidMeasurementShapeException("Length and width must be non-negative.")
104+
return length * width
105+
106+
def calculate_square_area(side):
107+
"""Calculate the area of a square."""
108+
if side < 0:
109+
raise InvalidMeasurementShapeException("Side must be non-negative.")
110+
return side * side
111+
112+
# Example usage:
113+
if __name__ == "__main__":
114+
# Simple calculator usage
115+
print("Please select operation -\n"
116+
"1. Add\n"
117+
"2. Subtract\n"
118+
"3. Multiply\n"
119+
"4. Divide\n")
120+
121+
try:
122+
select = int(input("Select operations from 1, 2, 3, 4: "))
123+
number_1 = float(input("Enter first number: "))
124+
number_2 = float(input("Enter second number: "))
125+
126+
if select == 1:
127+
print(f"{number_1} + {number_2} = {add(number_1, number_2)}")
128+
elif select == 2:
129+
print(f"{number_1} - {number_2} = {subtract(number_1, number_2)}")
130+
elif select == 3:
131+
print(f"{number_1} * {number_2} = {multiply(number_1, number_2)}")
132+
elif select == 4:
133+
result = divide(number_1, number_2)
134+
print(f"{number_1} / {number_2} = {result}")
135+
else:
136+
print("Invalid input, please select a number between 1 and 4.")
137+
138+
except ValueError:
139+
print("Error: Please enter valid numbers.")
140+

0 commit comments

Comments
 (0)