def roots4(a,b,c,d):
d = b * b - 4 * a * c
if a != 0 and d == 0:
roots4(a,b,c,d)
x = -b/ (2*a)
if a != 0 and d > 0:
roots4(a,b,c,d)
x1 = (-b + math.sqrt(d)) / 2.0 / a
x2 = (-b - math.sqrt(d)) / 2.0 / a
if a != 0 and d < 0:
roots4(a,b,c,d)
xre = (-b) / (2*a)
xim = (math.sqrt(d))/ (2*a)
print x1 = xre + ixim
strx1 = "x2 = %6.2f + i %6.2f" %(xre, xim)
print strx1
这是我项目代码的一部分。我要做的是定义roots4(a,b,c,d)
。例如,在a != 0 and d == 0
的情况下,那么roots4(a,b,c,d)
应该通过解方程x = -b/ (2*a)
找到x
。等等。。。我不知道我做错了什么。有什么建议吗?你知道吗
正如在评论和jimmy的回答中指出的,不需要递归调用。你知道吗
下面是一个示例,说明如何纠正它(以您喜欢的方式进行调整):
输出:
好吧,看起来你用递归函数做了一个无限循环。现在看来,你永远不会做任何计算除了
一遍又一遍。你知道吗
考虑程序流程。每次你到达
你会再次登上榜首的。你知道吗
你可能和
这会导致无限循环
首先,为什么需要递归调用?
d
参数的作用是什么?你知道吗其次,什么是
ixim
?应该是xim * 1j
之类的吗? 你对print x1 = xre + ixim
有什么期望?你知道吗如果您只想在
d < 0
的情况下打印,这就可以了相关问题 更多 >
编程相关推荐