Discuss / Python / 0#求解一元二次方程ax²+bx+c=0的两个解。

0#求解一元二次方程ax²+bx+c=0的两个解。

Topic source
import math
def quadratic(a, b, c):    if not isinstance(a + b + c, (int, float)):        raise TypeError('bad operand type')    z = math.pow(b, 2) - 4 * a * c    if z >= 0 and a != 0:        x1 = (-b + math.sqrt(z)) / (2 * a)        x2 = (-b - math.sqrt(z)) / (2 * a)        return x1, x2    if z >= 0 and a == 0 and b == 0 and c == 0:        print('x值为任意值')    if z >= 0 and a == 0 and b != 0:        x1 = -c / b        x2 = x1        return x1, x2    if z < 0:        print('方程无解')
import mathdef quadratic(a, b, c):    if not isinstance(a + b + c, (int, float)):        raise TypeError('bad operand type')    z = math.pow(b, 2) - 4 * a * c    if z >= 0 and a != 0:        x1 = (-b + math.sqrt(z)) / (2 * a)        x2 = (-b - math.sqrt(z)) / (2 * a)        return x1, x2    if z >= 0 and a == 0 and b == 0 and c == 0:        print('x值为任意值')    if z >= 0 and a == 0 and b != 0:        x1 = -c / b        x2 = x1        return x1, x2    if z < 0:        print('方程无解')

  • 1

Reply