简体   繁体   English

将列表从一个功能传递到另一个功能

[英]Passing a list from one function to another

I need help understanding this instruction. 我需要帮助来理解此说明。

Your main program code should call the “getData” function. 您的主程序代码应调用“ getData”函数。 Pass the list returned from the “summer” function to “getData” and save the sum that function returns. 将“ summer”函数返回的列表传递给“ getData”,并保存该函数返回的总数。

def getData():
    fin = open("sample.dat","r")
    numbers=[]
    for line in fin:
        val =line.rstrip()
        numbers.append(val)
    return numbers
def summer(lst):
    sum=0
    for n in range(0,13,2):
        sum+=powerval(lst[n],lst[n+1])
    return sum

If the instruction read, 如果阅读说明,

Pass the list returned by the getData function to summer and save the sum that function returns. getData函数返回的列表传递给summer并保存该函数返回的总和。

then try: 然后尝试:

total = summer(getData())

I think this satisfies the requirements. 我认为这可以满足要求。

def getData(lst):
    for l in lst:
        do something with sum
    save sum
    return sum

invoke as:
    getData(summer(some_other_data))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM