从另一个函数计算平均值的Python函数

2024-04-24 19:43:45 发布

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

我无法得到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)

Tags: andofthe函数returndefrandomreturns
1条回答
网友
1楼 · 发布于 2024-04-24 19:43:45

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

现在,当您在randomAverage内调用average时,函数(average)工作正常并返回它应该返回的内容。但是,您的代码到此为止。如果在randomAverage中没有返回average返回的内容的return语句,average返回的值将被忽略。你知道吗

相关问题 更多 >