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')
sqrt = b b - 4 a c
if a == 0:
return -b / c
if sqrt < 0:
return "无实数解"
else:
x1 = (-b + math.sqrt(sqrt)) / 2 a
x2 = (-b - math.sqrt(sqrt)) / 2 * a
return x1, x2
草莓Z葵
#!/usr/bin/env python3
-- coding: utf-8 --
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') sqrt = b b - 4 a c if a == 0: return -b / c if sqrt < 0: return "无实数解" else: x1 = (-b + math.sqrt(sqrt)) / 2 a x2 = (-b - math.sqrt(sqrt)) / 2 * a return x1, x2