**我试图让函数返回二次方程的结果,但我不知道如何将解打印为分数。请帮帮我!**
def cube_root(x):
return x**(1/3)
def Quadratic(a, b, c):
delta = (b**2)-4*a*c
if delta == 0:
x = (-b)/2*a
return f"This Quadratic equation has 1 solution: {x}"
else:
if delta < 0 :
return "This Quadratic equation has no solutions: "
else:
x1 = ((-b)-cube_root(delta))/2*a
x2 = ((-b)+cube_root(delta))/2*a
return f"This Quadratic equation has 2 solutions: {x1} & {x2}"
print(Quadratic(12, 0, -1))
如果您不需要额外的软件包,以下内容可能会有所帮助:
测试:
您可以从
sympy
包中使用simplify
(不在标准库中-您必须安装它):这使得:
不同的例子:
给出:
实际上
sympy
也可以为您处理复数,这样您就可以摆脱没有实际解决方案的测试(即删除elif
,这样delta < 0
由else:
块处理)如果您这样做,然后给出一个示例:
你会得到:
相关问题 更多 >
编程相关推荐