diff --git a/Flowchart for quadratic solver assignment.jpg b/Flowchart for quadratic solver assignment.jpg new file mode 100644 index 0000000..4c430f6 Binary files /dev/null and b/Flowchart for quadratic solver assignment.jpg differ diff --git a/README.md b/README.md index aa4e50b..e48b4df 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,69 @@ -# pythonlab +#pythonlab -Python Lab + +Group_13 +Group Report on Quadratic Equation Solver + +Group Members: +Lohedami MOGBOLU [ENG2403540], +ORITSEGBUGBEMI Oritsesholaja Ephraim [ENG2403565], +GODFREY-ARIAVE Jason [ENG2403523], +UMOH Mmekobong Ime [ENG2403576], +AGINDOTAN Boyowa Solomon [ENG2410267], +OKPISA Ilamosi Theresa [ENG2403557], +OHAHMEN Superior Afudah [ENG2403553] + +Date: Sept. 2025 + +Course: CPE112 + +Lecturer: Dr. E. Olaye + +INTRODUCTION +This report documents the team work in writing, implementing, debugging and testing of a +Python program to find the roots of the quadratic equation 𝑎𝑥2 + 𝑏𝑥 + 𝑐 = 0 using x=-b ± ✓(b² - 4ac)/2a +The program, developed by Group_13 handles two cases: two normal roots and two complex roots. The work was implemented using Visual Studio Code, tested with Pytest and using draw.io, the flowchart was designed. After which it was documented using MS Word. + + ORITSEGBUGBEMI Oritsesholaja Ephraim implemented the Python code and also tested the program using the Pytest framework alongside UMOH Mmekobong Ime. + + Lohedami MOGBOLU documented the process. + + GODFREY-ARIAVE Jason designed the flowchart. + +FLOWCHART DEVELOPMENT +The flowchart was designed to outline the procedure used in solving the quadratic equation ax² + bx + c = 0. +GODFREY-ARIAVE Jason led this task, using the online flowchart tool draw.io: +• Step 1- Start: The program begins here. +•Step 2 - Input: The user is asked to input their values for each unknown. + +•Step 3 - The discriminant 𝑑 =b² − 4𝑎𝑐 is calculated. And, based on d the roots are calculated: + If d >= 0: compute x1, x2 and output otherwise print complex roots found. +•Step 4 - End: The program ends here. + + +IMPLEMENTATION + +The program was implemented by ORITSEGBUGBEMI Oritsesholaja Ephraim using VS Code + ● Code structure: +A quadratic_solver() function computes and displays roots based on the +discriminant. +A get_positive_float_input() function ensures the user does not input any number less than 1. +A get_float_input() function validates numeric input with a while loop and try-except block. + + The file was saved as quadratic_solver.py and tested. + +TESTING +The program was tested by ORITSEGBUGBEMI Oritsesholaja Ephraim, Lohedami MOGBOLU and UMOH Mmekobong Ime using the Pytest framework. This was done in two stages: + +Stage 1: The Manual Testing Stage +Test case 1: a = 1; b = 3; c = 1. Two real roots were found: -0.3819 and -2.6180. +Tested by Lohedami MOGBOLU. +Test Case 2: a = 2; b = 4; c = 4. Two complex roots were found: -1 + 4i and -1 – 4i. +Tested by Lohedami MOGBOLU + +Stage 2: The Automated Testing Stage. +ORITSEGBUGBEMI Oritsesholaja Ephraim created test_quadratic_solver.py All tests passed as reviewed by the group. + +CONCLUSION +The quadratic equation solver was successfully created. Lohedami MOGBOLU after executing the code observed there was no if statement should a user decide to input zero as the coefficient of x². She suggested that to ORITSEGBUGBEMI Oritsesholaja Ephraim. UMOH Mmekobong Ime tested the initial file program but when Lohedami MOGBOLU suggested an edit, ORITSEGBUBEMI Oritsesholaja Ephraim stepped up to rewrite the final test file. + diff --git a/quadratic_solver.py b/quadratic_solver.py new file mode 100644 index 0000000..f57891d --- /dev/null +++ b/quadratic_solver.py @@ -0,0 +1,53 @@ +def quadratic_solver(a: float, b: float, c: float): + """ + Solves quadratic equation ax^2 + bx + c = 0 + Returns tuple (x1, x2) if real roots, or string if complex roots. + Note: a must be > 0 + """ + if a <= 0: + raise ValueError("Coefficient 'a' must be greater than 0 for a quadratic equation.") + + d = b**2 - 4*a*c # discriminant + + if d >= 0: + x1 = (-b + d**0.5) / (2*a) + x2 = (-b - d**0.5) / (2*a) + return (x1, x2) + else: + return "complex roots found" + + +def get_positive_float_input(prompt: str) -> float: + """Keep asking until user enters a valid number greater than 0.""" + while True: + try: + value = float(input(prompt)) + if value > 0: + return value + else: + print("❌ Coefficient 'a' must be greater than 0.") + except ValueError: + print("❌ Invalid input! Please enter a number.") + + +def get_float_input(prompt: str) -> float: + """Keep asking until user enters a valid number (can be negative or positive).""" + while True: + try: + return float(input(prompt)) + except ValueError: + print("❌ Invalid input! Please enter a number.") + + +if __name__ == "__main__": + print("Quadratic Equation Solver: ax² + bx + c = 0") + a = get_positive_float_input("Enter coefficient a (must be > 0): ") + b = get_float_input("Enter coefficient b: ") + c = get_float_input("Enter coefficient c: ") + + result = quadratic_solver(a, b, c) + + if isinstance(result, tuple): + print(f"The roots are: x1 = {result[0]}, x2 = {result[1]}") + else: + print(result) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e079f8a --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pytest diff --git a/test_quadratic_solver.py b/test_quadratic_solver.py new file mode 100644 index 0000000..f34c461 --- /dev/null +++ b/test_quadratic_solver.py @@ -0,0 +1,24 @@ +import pytest +from quadratic_solver import quadratic_solver + +def test_real_distinct_roots(): + result = quadratic_solver(1, -5, 6) # roots 2 and 3 + assert pytest.approx(result[0]) in (2, 3) + assert pytest.approx(result[1]) in (2, 3) + +def test_real_equal_roots(): + result = quadratic_solver(1, -4, 4) # root 2 (double) + assert result == (2, 2) + +def test_complex_roots(): + result = quadratic_solver(1, 1, 1) # complex roots + assert result == "complex roots found" + +def test_large_coefficients(): + result = quadratic_solver(2, -8, 8) # root 2 (double) + assert result == (2, 2) + +def test_negative_a(): + result = quadratic_solver(-1, 3, -2) # roots 1 and 2 + assert pytest.approx(result[0]) in (1, 2) + assert pytest.approx(result[1]) in (1, 2)