This repository has been archived on 2024-10-24. You can view files and clone it, but cannot push or open issues or pull requests.
NG_2022_Stanislav_Mykhailenko/Lesson_1/task4.py

37 lines
801 B
Python
Raw Normal View History

2022-10-18 14:54:32 +00:00
# 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)
2022-10-18 17:57:16 +00:00
if not math.isfinite(a) or not math.isfinite(b) or not math.isfinite(c):
print("Invalid numbers entered.")
sys.exit(1)
2022-10-18 14:54:32 +00:00
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)))