使用ifstatemen定义函数

2024-10-03 04:28:27 发布

您现在位置:Python中文网/ 问答频道 /正文

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。等等。。。我不知道我做错了什么。有什么建议吗?你知道吗


Tags: and项目代码ifdefmathsqrtprint
3条回答

正如在评论和jimmy的回答中指出的,不需要递归调用。你知道吗

下面是一个示例,说明如何纠正它(以您喜欢的方式进行调整):

#!/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)

输出:

$ ./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

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

d = b * b - 4 * a * c

一遍又一遍。你知道吗

考虑程序流程。每次你到达

roots4(a,b,c,d)

你会再次登上榜首的。你知道吗

你可能和

    ...
            roots4(a,b,c,d)
    ...

这会导致无限循环

首先,为什么需要递归调用?d参数的作用是什么?你知道吗

其次,什么是ixim?应该是xim * 1j之类的吗? 你对print x1 = xre + ixim有什么期望?你知道吗

如果您只想在d < 0的情况下打印,这就可以了

    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")

相关问题 更多 >