From 0d96e727fb0635359facb7726dce4f91c49e0fe3 Mon Sep 17 00:00:00 2001 From: Stanislav Mykhailenko Date: Tue, 18 Oct 2022 17:54:32 +0300 Subject: [PATCH] Add Lesson 1 Task 4 --- Lesson_1/task4.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Lesson_1/task4.py diff --git a/Lesson_1/task4.py b/Lesson_1/task4.py new file mode 100644 index 0000000..1bad800 --- /dev/null +++ b/Lesson_1/task4.py @@ -0,0 +1,32 @@ +# Lesson 1 Task 4: quadratic equation calculator +# Author: Stanislav Mykhailenko +# License: Unlicense + +# Return codes: +# 0 - OK +# 1 - Invalid numbers entered +# 2 - Not a quadratic equation + +import sys, math + +try: + a = float(input("Enter number a: ")) + b = float(input("Enter number b: ")) + c = float(input("Enter number c: ")) +except ValueError: + print("Invalid numbers entered.") + sys.exit(1) + +if a == 0: + print("Not a quadratic equation.") + sys.exit(2) + +# Discriminant +D = math.pow(b, 2) - 4 * a * c + +if D < 0: + print("No real roots exist.") +elif D >= 0: + print("x₁ = " + str((-b + math.sqrt(D)) / (2 * a))) + if D > 0: + print("x₂ = " + str((-b - math.sqrt(D)) / (2 * a)))