练习
请定义一个函数quadratic(a, b, c)
,接收3个参数,返回一元二次方程:
ax2 + bx + c = 0
的两个解。
import math
def quadratic(a, b, c):
if not isinstance(a, (int, float)):
raise TypeError('bad operand type')
if not isinstance(b, (int, float)):
raise TypeError('bad operand type')
if not isinstance(c, (int, float)):
raise TypeError('bad operand type')
delta = b*b - 4*a*c
if delta < 0:
print("error: no real root")
elif delta == 0:
print("only one repeated root")
x = (-b) / (2*a)
else:
print("two root:")
x1 = (-b - math.sqrt(delta)) / (2*a)
x2 = (-b + math.sqrt(delta)) / (2*a)
print(x1, x2)