如何在Python的另一个函数中使用以前编写的函数?

2024-04-23 17:33:43 发布

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

我正在使用名为Wing IDE的Python编码程序。你知道吗

我创建了一个函数,它生成一个n个随机数的列表,其中的数字介于0和10之间。我现在试着写一个函数来计算一组数字的平均值。你知道吗

我想在新函数中使用以前的函数(名为randomNumbers(n)),但我不知道如何调用该函数并在新函数中运行它。你知道吗

有人能帮忙吗?你知道吗

例如,我有一个新函数calculateAverage(n),我试图在calculateAverage(n)内运行randomNumbers(n),然后找到randomNumbers(n)创建的数字列表的平均值。你知道吗


Tags: 函数程序编码列表数字ide平均值wing
1条回答
网友
1楼 · 发布于 2024-04-23 17:33:43

调用函数的语法是functionName(arg1, arg2, ...)。你知道吗

def calculate_average(n):
    # call the function and store the result.
    random_list = randomNumbers(n)

    average = 0
    # iterate through random_list, add each number to average

    return average / n

或者更干净

 def calculate_average(n):
     random_sum = sum(randomNumbers(n))
     # now you have the sum of the list of numbers,
     # getting the average is simple

相关问题 更多 >