首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用if-statement定义函数

使用if-statement定义函数
EN

Stack Overflow用户
提问于 2016-07-29 14:14:57
回答 3查看 68关注 0票数 0
代码语言:javascript
复制
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的情况下。以此类推。我不知道我做错了什么。有什么建议吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-07-29 15:49:48

正如评论和jimmy的回答中所指出的,不需要递归调用。

这里有一个如何纠正它的例子(以你喜欢的方式调整它):

代码语言:javascript
复制
#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import math


def roots4(a, b, c):
    """Prints the solutions of ax² + bx + c = 0, if a != 0"""
    if a == 0:
        print 'This function is meant to solve a 2d degree equation, '\
            'not a 1st degree one.'
    else:
        d = b * b - 4 * a * c
        solutions = []
        if d == 0:
            solutions = [str(-b / (2 * a))]
        elif d > 0:
            solutions = [str((-b + math.sqrt(d)) / 2.0 / a),
                         str((-b - math.sqrt(d)) / 2.0 / a)]
        elif d < 0:
            xre = str((-b) / (2 * a))
            xim = str((math.sqrt(-d)) / (2 * a))
            solutions = [xre + " + i" + xim,
                         xre + " - i" + xim]

        print "\nEquation is: {}x² + {}x + {} = 0".format(a, b, c)

        if len(solutions) == 1:
            print "There's only one solution: " + solutions[0]
        else:
            print "Solutions are: " + " and ".join(solutions)

roots = [(0.0, 0.0, 0.0),
         (0.0, 0.0, 1.0),
         (0.0, 2.0, 4.0),
         (1.0, 2.0, 1.0),
         (1.0, -5.0, 6.0),
         (1.0, 2.0, 3.0)]

for r in roots:
    roots4(*r)

输出:

代码语言:javascript
复制
$ ./test_script2.py
This function is meant to solve a 2d degree equation, not a 1st degree one.
This function is meant to solve a 2d degree equation, not a 1st degree one.
This function is meant to solve a 2d degree equation, not a 1st degree one.

Equation is: 1.0x² + 2.0x + 1.0 = 0
There's only one solution: -1.0

Equation is: 1.0x² + -5.0x + 6.0 = 0
Solutions are: 3.0 and 2.0

Equation is: 1.0x² + 2.0x + 3.0 = 0
Solutions are: -1.0 + i1.41421356237 and -1.0 - i1.41421356237
票数 0
EN

Stack Overflow用户

发布于 2016-07-29 16:05:47

你很可能堆积了

代码语言:javascript
复制
    ...
            roots4(a,b,c,d)
    ...

这会导致无限循环。

首先,为什么你需要一个递归调用?d参数是做什么用的?

其次,什么是ixim?它应该是像xim * 1j一样的东西吗?你对print x1 = xre + ixim有什么期望?

如果你只想在d < 0的情况下打印,这是很好的

代码语言:javascript
复制
    from math import sqrt


    def roots4(a,b,c):
        if a != 0.:
            x_left = -b/(2*a)

            d = b * b - 4 * a * c
            if d == 0.:
                x_right = 0.
            elif d > 0.:
                x_right = sqrt(d) / (2 * a)
            else:
                xim = sqrt(-d) / (2 * a)
                strx1 = "x1 = %6.2f + i %6.2f" %(x_left, xim)
                print strx1
                strx2 = "x2 = %6.2f - i %6.2f" %(x_left, xim)
                print strx2
                x_right = xim * 1j
            x1 = x_left + x_right
            x2 = x_left - x_right
        else:
            raise ValueError("incorrect leading coefficient in given square equation")
票数 1
EN

Stack Overflow用户

发布于 2016-07-29 15:31:18

好的,看起来你已经用你的递归函数做了一个无限循环。现在看来,你永远不会做任何计算,除了

代码语言:javascript
复制
d = b * b - 4 * a * c

一遍又一遍。

考虑程序流。每次你到达

代码语言:javascript
复制
roots4(a,b,c,d)

你最终会再次名列前茅。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38651714

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档