for Coef in (a, b, c):
if not isinstance(Coef, (int, float)):
raise TypeError('bad operand type')
# 计算判别式(delta)
delta = b**2 - 4*a*c
# 判断方程有多少解
if delta > 0:
# 有两个实根
x1 = (-b-math.sqrt(delta ))/(2*a)
x2 = (-b+math.sqrt(delta ))/(2*a)
return x1, x2
elif delta == 0:
# 有一个实根
x = -b / (2*a)
return x
else:
# 无实根
return None
Sign in to make a reply
SPA
for Coef in (a, b, c):
if not isinstance(Coef, (int, float)):
raise TypeError('bad operand type')
# 计算判别式(delta)
delta = b**2 - 4*a*c
# 判断方程有多少解
if delta > 0:
# 有两个实根
x1 = (-b-math.sqrt(delta ))/(2*a)
x2 = (-b+math.sqrt(delta ))/(2*a)
return x1, x2
elif delta == 0:
# 有一个实根
x = -b / (2*a)
return x
else:
# 无实根
return None