From 6bab1dd9f193b6be5e7efadc5c427d0ff4459532 Mon Sep 17 00:00:00 2001 From: KOTLA SAI SAKETH <114326635+kotla-saketh@users.noreply.github.com> Date: Tue, 22 Oct 2024 17:26:44 +0530 Subject: [PATCH 1/4] 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. --- easyPythonpi/easyPythonpi.py | 126 +++++++++++++++++++++++++++++++++-- 1 file changed, 121 insertions(+), 5 deletions(-) diff --git a/easyPythonpi/easyPythonpi.py b/easyPythonpi/easyPythonpi.py index a963b89..1a8fd7b 100644 --- a/easyPythonpi/easyPythonpi.py +++ b/easyPythonpi/easyPythonpi.py @@ -11,14 +11,130 @@ # define exception for invalid Binary Strings +# class InvalidBinaryException(Exception): +# pass + +# class InvalidNumberFibException(Exception): +# def __init__(self, n, message="n is not valid, must be greater than or equal to 1"): +# self.n = n +# self.message = message + +# class InvalidMeasurementShapeException(Exception): +# def __init__(self, message="invalid measurement, must be greater than or equal to 0"): +# self.message = message + +"""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.""" + + +# Define exception for invalid binary strings class InvalidBinaryException(Exception): + """Raised when an invalid binary string is provided.""" pass +# Define exception for invalid Fibonacci numbers class InvalidNumberFibException(Exception): + """Raised when the Fibonacci number is invalid.""" def __init__(self, n, message="n is not valid, must be greater than or equal to 1"): - self.n = n - self.message = message - + self.n = n + self.message = message + super().__init__(self.message) + +# Define exception for invalid measurements for shapes class InvalidMeasurementShapeException(Exception): - def __init__(self, message="invalid measurement, must be greater than or equal to 0"): - self.message = message \ No newline at end of file + """Raised when invalid measurement values are provided for shapes.""" + def __init__(self, message="Invalid measurement, must be greater than or equal to 0"): + self.message = message + super().__init__(self.message) + +# Functions for basic calculations + +def add(num1, num2): + """Add two numbers.""" + return num1 + num2 + +def subtract(num1, num2): + """Subtract two numbers.""" + return num1 - num2 + +def multiply(num1, num2): + """Multiply two numbers.""" + return num1 * num2 + +def divide(num1, num2): + """Divide two numbers with error handling for division by zero.""" + try: + return num1 / num2 + except ZeroDivisionError: + return "Error: Division by zero is not allowed." + +# Binary string to decimal conversion +def binary_to_decimal(binary_str): + """Convert binary string to decimal.""" + try: + # Check if the binary string is valid + if not set(binary_str).issubset({'0', '1'}): + raise InvalidBinaryException(f"Invalid binary string: {binary_str}") + return int(binary_str, 2) + except InvalidBinaryException as e: + return str(e) + +# Fibonacci sequence generator +def fibonacci(n): + """Generate Fibonacci sequence up to the nth number.""" + if n < 1: + raise InvalidNumberFibException(n) + + fib_sequence = [0, 1] + for i in range(2, n): + fib_sequence.append(fib_sequence[-1] + fib_sequence[-2]) + return fib_sequence[:n] + +# Shape area calculators + +def calculate_circle_area(radius): + """Calculate the area of a circle given the radius.""" + if radius < 0: + raise InvalidMeasurementShapeException("Radius must be non-negative.") + return 3.14159 * radius * radius + +def calculate_rectangle_area(length, width): + """Calculate the area of a rectangle.""" + if length < 0 or width < 0: + raise InvalidMeasurementShapeException("Length and width must be non-negative.") + return length * width + +def calculate_square_area(side): + """Calculate the area of a square.""" + if side < 0: + raise InvalidMeasurementShapeException("Side must be non-negative.") + return side * side + +# Example usage: +if __name__ == "__main__": + # Simple calculator usage + print("Please select operation -\n" + "1. Add\n" + "2. Subtract\n" + "3. Multiply\n" + "4. Divide\n") + + try: + select = int(input("Select operations from 1, 2, 3, 4: ")) + number_1 = float(input("Enter first number: ")) + number_2 = float(input("Enter second number: ")) + + if select == 1: + print(f"{number_1} + {number_2} = {add(number_1, number_2)}") + elif select == 2: + print(f"{number_1} - {number_2} = {subtract(number_1, number_2)}") + elif select == 3: + print(f"{number_1} * {number_2} = {multiply(number_1, number_2)}") + elif select == 4: + result = divide(number_1, number_2) + print(f"{number_1} / {number_2} = {result}") + else: + print("Invalid input, please select a number between 1 and 4.") + + except ValueError: + print("Error: Please enter valid numbers.") + From 8ace2234235f6a33d989bbf23ebf8235daf7586d Mon Sep 17 00:00:00 2001 From: KOTLA SAI SAKETH <114326635+kotla-saketh@users.noreply.github.com> Date: Tue, 22 Oct 2024 21:01:50 +0530 Subject: [PATCH 2/4] 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. --- easyPythonpi/easyPythonpi.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/easyPythonpi/easyPythonpi.py b/easyPythonpi/easyPythonpi.py index 1a8fd7b..609b89a 100644 --- a/easyPythonpi/easyPythonpi.py +++ b/easyPythonpi/easyPythonpi.py @@ -26,6 +26,19 @@ """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.""" +""" +A python module that helps you to calculate some of the most used calculations. + +Usage: + Download the file from GitHub and unzip it on your system. + To use this module, simply write the following in your code: + 'from easypythonpi import *' and you are good to go! + + ~Happy Programming +""" + +# Programmer-defined exceptions go here: + # Define exception for invalid binary strings class InvalidBinaryException(Exception): """Raised when an invalid binary string is provided.""" From 6e0fc9e440ea0b2a238fe1ce696163ff33892724 Mon Sep 17 00:00:00 2001 From: KOTLA SAI SAKETH <114326635+kotla-saketh@users.noreply.github.com> Date: Sat, 26 Oct 2024 10:26:39 +0530 Subject: [PATCH 3/4] solution of easyPythonpi.py A python module that helps you to calculate some of the most used calculations..... --- easyPythonpi/easyPythonpi.py | 107 ++++------------------------------- 1 file changed, 12 insertions(+), 95 deletions(-) diff --git a/easyPythonpi/easyPythonpi.py b/easyPythonpi/easyPythonpi.py index 609b89a..d0e9fe8 100644 --- a/easyPythonpi/easyPythonpi.py +++ b/easyPythonpi/easyPythonpi.py @@ -1,4 +1,3 @@ - """ A python module that helps you to calculate some of the most used calculations..... usage-- Just download the file from git and unzip in ur system. @@ -11,35 +10,19 @@ # define exception for invalid Binary Strings -# class InvalidBinaryException(Exception): -# pass +"""class InvalidBinaryException(Exception): + pass -# class InvalidNumberFibException(Exception): -# def __init__(self, n, message="n is not valid, must be greater than or equal to 1"): -# self.n = n -# self.message = message +class InvalidNumberFibException(Exception): + def __init__(self, n, message="n is not valid, must be greater than or equal to 1"): + self.n = n + self.message = message -# class InvalidMeasurementShapeException(Exception): -# def __init__(self, message="invalid measurement, must be greater than or equal to 0"): -# self.message = message - -"""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.""" - - -""" -A python module that helps you to calculate some of the most used calculations. - -Usage: - Download the file from GitHub and unzip it on your system. - To use this module, simply write the following in your code: - 'from easypythonpi import *' and you are good to go! - - ~Happy Programming -""" - -# Programmer-defined exceptions go here: - -# Define exception for invalid binary strings +class InvalidMeasurementShapeException(Exception): + def __init__(self, message="invalid measurement, must be greater than or equal to 0"): + self.message = message""" +"""--------------------------------------------------------""" +#program starts from here class InvalidBinaryException(Exception): """Raised when an invalid binary string is provided.""" pass @@ -58,71 +41,6 @@ class InvalidMeasurementShapeException(Exception): def __init__(self, message="Invalid measurement, must be greater than or equal to 0"): self.message = message super().__init__(self.message) - -# Functions for basic calculations - -def add(num1, num2): - """Add two numbers.""" - return num1 + num2 - -def subtract(num1, num2): - """Subtract two numbers.""" - return num1 - num2 - -def multiply(num1, num2): - """Multiply two numbers.""" - return num1 * num2 - -def divide(num1, num2): - """Divide two numbers with error handling for division by zero.""" - try: - return num1 / num2 - except ZeroDivisionError: - return "Error: Division by zero is not allowed." - -# Binary string to decimal conversion -def binary_to_decimal(binary_str): - """Convert binary string to decimal.""" - try: - # Check if the binary string is valid - if not set(binary_str).issubset({'0', '1'}): - raise InvalidBinaryException(f"Invalid binary string: {binary_str}") - return int(binary_str, 2) - except InvalidBinaryException as e: - return str(e) - -# Fibonacci sequence generator -def fibonacci(n): - """Generate Fibonacci sequence up to the nth number.""" - if n < 1: - raise InvalidNumberFibException(n) - - fib_sequence = [0, 1] - for i in range(2, n): - fib_sequence.append(fib_sequence[-1] + fib_sequence[-2]) - return fib_sequence[:n] - -# Shape area calculators - -def calculate_circle_area(radius): - """Calculate the area of a circle given the radius.""" - if radius < 0: - raise InvalidMeasurementShapeException("Radius must be non-negative.") - return 3.14159 * radius * radius - -def calculate_rectangle_area(length, width): - """Calculate the area of a rectangle.""" - if length < 0 or width < 0: - raise InvalidMeasurementShapeException("Length and width must be non-negative.") - return length * width - -def calculate_square_area(side): - """Calculate the area of a square.""" - if side < 0: - raise InvalidMeasurementShapeException("Side must be non-negative.") - return side * side - -# Example usage: if __name__ == "__main__": # Simple calculator usage print("Please select operation -\n" @@ -147,7 +65,6 @@ def calculate_square_area(side): print(f"{number_1} / {number_2} = {result}") else: print("Invalid input, please select a number between 1 and 4.") - + except ValueError: print("Error: Please enter valid numbers.") - From a6bea383a41200bf2ef48bcf0907ac71f22690f6 Mon Sep 17 00:00:00 2001 From: KOTLA SAI SAKETH <114326635+kotla-saketh@users.noreply.github.com> Date: Sat, 26 Oct 2024 11:13:55 +0530 Subject: [PATCH 4/4] solution of easyPythonpi.py HACKTOBERFEST --- easyPythonpi/easyPythonpi.py | 50 +++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/easyPythonpi/easyPythonpi.py b/easyPythonpi/easyPythonpi.py index d0e9fe8..147095d 100644 --- a/easyPythonpi/easyPythonpi.py +++ b/easyPythonpi/easyPythonpi.py @@ -1,33 +1,16 @@ -""" A python module that helps you to calculate some of the most used calculations..... +""" A Python module that helps you to calculate some of the most used calculations..... usage-- - Just download the file from git and unzip in ur system. + Just download the file from git and unzip it on your system. And while using this module, just write as code- - 'from easypythonpi import *' and u r good to go... - ~Happy programming""" - + 'from easypythonpi import *' and you're good to go... + ~Happy programming +""" -# Programmer defined exceptions go here: - -# define exception for invalid Binary Strings - -"""class InvalidBinaryException(Exception): - pass - -class InvalidNumberFibException(Exception): - def __init__(self, n, message="n is not valid, must be greater than or equal to 1"): - self.n = n - self.message = message - -class InvalidMeasurementShapeException(Exception): - def __init__(self, message="invalid measurement, must be greater than or equal to 0"): - self.message = message""" -"""--------------------------------------------------------""" -#program starts from here +# Programmer-defined exceptions class InvalidBinaryException(Exception): """Raised when an invalid binary string is provided.""" pass -# Define exception for invalid Fibonacci numbers class InvalidNumberFibException(Exception): """Raised when the Fibonacci number is invalid.""" def __init__(self, n, message="n is not valid, must be greater than or equal to 1"): @@ -35,12 +18,31 @@ def __init__(self, n, message="n is not valid, must be greater than or equal to self.message = message super().__init__(self.message) -# Define exception for invalid measurements for shapes class InvalidMeasurementShapeException(Exception): """Raised when invalid measurement values are provided for shapes.""" def __init__(self, message="Invalid measurement, must be greater than or equal to 0"): self.message = message super().__init__(self.message) + +# Function definitions +def add(x, y): + """Return the sum of x and y.""" + return x + y + +def subtract(x, y): + """Return the difference of x and y.""" + return x - y + +def multiply(x, y): + """Return the product of x and y.""" + return x * y + +def divide(x, y): + """Return the quotient of x and y, or an error message if dividing by zero.""" + if y == 0: + return "Cannot divide by zero" + return x / y + if __name__ == "__main__": # Simple calculator usage print("Please select operation -\n"