from math import sqrt def get_roots(a, b, c): # a=0 makes the function linear, not quadratic. if a == 0: return None, None else: discriminant = b ** 2 - 4 * a * c # The square root is extracted only from a nonnegative number. if discriminant < 0: return None, None # If discriminant is 0, then roots of the quadratic equation 1. elif discriminant == 0: root1 = (-b - sqrt(discriminant)) / (2 * a) return root1, None # If discriminant > 0, then roots of the quadratic equation 2. else: root1 = (-b - sqrt(discriminant)) / (2 * a) root2 = (-b + sqrt(discriminant)) / (2 * a) return root1, root2
Код работает верно ,но нужно обойтись без вложенных if-ов и без использования root1 два раза.
Пробую сделать двумя функциями, но возникает ошибка:
TypeError: 'NoneType' object is not iterable
def get_roots(a, b, c): discriminant = b ** 2 - 4 * a * c if discriminant < 0: return None, None else: roots(a, b, discriminant) def roots(a, b, d): root1 = (-b - sqrt(d)) / (2 * a) root2 = (-b + sqrt(d)) / (2 * a) if d == 0: return root1, None else: return root1, root2