Python函数,用于计算另一个函数的平均值

时间:2013-10-14 18:05:05

标签: python average

我无法获得randomCrosses函数的返回值(a,b,c,d),以便在randomAverage函数中一起使用时输入平均函数。有人请告诉我我错过了什么!

def randomCrosses():
    """Draws four random crosses of randomized values between 0-400 and returns the four random values a,b,c,d""" 
    a = r.randint(0,400)
    drawCross("Darkgreen",(a, 10))
    b = r.randint(0,400)
    drawCross("blue",(b, 10))
    c = r.randint(0,400)
    drawCross("magenta",(c, 10))
    d = r.randint(0,400)
    drawCross("limegreen",(d, 10))
    return(a,b,c,d)


def average(a,b,c,d):
    """Calculates and returns the average of four values a,b,c,d"""
    mean = (a+b+c+d)/4
    return mean


def randomAverage():
    """Randomizes four values 0-400 for a,b,c,d and then calculates the average of these values""" 
    a,b,c,d = randomCrosses()
    average(a,b,c,d)

1 个答案:

答案 0 :(得分:0)

您错过了randomAverage中的return语句:

def randomAverage():
    """Randomizes four values 0-400 for a,b,c,d and then calculates the average of these values""" 
    a,b,c,d = randomCrosses()
    return average(a,b,c,d)
    #  ^-- you need this

现在,当您在average内调用randomAverage时,函数(average)可以正常工作并返回应有的内容。但是,您的代码会停在那里。如果randomAverage内没有返回average返回的返回语句,则average返回的值将被忽略。