project_python/2.2_lesson.17.20.py

39 lines
1.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

''' ****** Задание 17(Q) из 20 Параграф 2.2 *********
* Знаете ли вы, что Python изначально создавался для решения математических задач?
Давайте воспользуемся его возможностями и найдём корни уравнения, используя составные условные операции.
'''
if __name__ == "__main__"
import math
try:
a = float(input())
b = float(input())
c = float(input())
except ValueError:
print("Ошибка: введите корректные числовые значения.")
exit()
if a == 0 and b == 0:
if c == 0:
print("Infinite solutions")
else:
print("No solution")
elif a == 0:
root = -c / b
print(f"{root:.2f}")
else:
discriminant = b ** 2 - 4 * a * c
if discriminant < 0:
print("No solution")
elif discriminant == 0:
root = -b / (2 * a)
print(f"{root:.2f}")
else:
root1 = (-b - math.sqrt(discriminant)) / (2 * a)
root2 = (-b + math.sqrt(discriminant)) / (2 * a)
if root1 > root2:
root1, root2 = root2, root1
print(f"{root1:.2f} {root2:.2f}")