使用if语句

时间:2016-07-29 06:14:57

标签: python python-2.7 function if-statement printing

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来查找x = -b/ (2*a)。等等......我不知道我做错了什么。有什么提示吗?

3 个答案:

答案 0 :(得分:1)

你可能与

叠加在一起
    ...
            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")

答案 1 :(得分:0)

好吧,看起来你已经使用递归函数进行了无限循环。现在看起来除了

之外你永远不会做任何计算
Notepad

一遍又一遍。

考虑程序流程。每次到达

d = b * b - 4 * a * c

你将再次登顶。

答案 2 :(得分:0)

正如评论和吉米的答案所指出的那样,不需要递归调用。

这是一个如何纠正它的例子(按照你喜欢的方式调整它):

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