欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

请定义一个函数quadratic(a, b, c),接收3个参数,返回一元二次方程 ax^2+bx+c=0ax 2 +bx+c=0 的两个解。

程序员文章站 2022-11-08 13:45:23
#!/usr/bin/python # 导入math包 import math def quadratic(a, b, c): if not isinstance(a, (int, float))and isinstance(a, (int, float)) and isinstance(a, (i ......
#!/usr/bin/python
# 导入math包
import math


def quadratic(a, b, c):
if not isinstance(a, (int, float))and isinstance(a, (int, float)) and isinstance(a, (int, float)):
raise typeerror('a or b or c must be a number')
dt = int(b) ** 2-(4*int(a)*int(c))
if a == 0:
if b == 0:
if c == 0:
return'all real number'
else:
return 'a can\'t be 0,该方程没有实数解'
else:
x1 = -c/b
x2 = x1
return x1, x2
else:
if dt < 0:
return '该方程没有实数解'
elif dt == 0:
x = -b/(2*a)
return x
else:
x3 = (-b+math.sqrt(dt))/(2*a)
x4 = (-b-math.sqrt(dt))/(2*a)
return x3, x4


a1 = input('a=',)
b1 = input('b=',)
c1 = input('c=',)
print('ax^2+bx+c=0的解为:', quadratic(a1, b1, c1))