如何将列表从一个功能传递给另一个功能?

时间:2020-02-05 19:24:22

标签: python python-3.x list function

问题:玩弄在函数之间交换变量的想法。

我正在执行以下代码:

def benefits():
    list =  ["Beautiful", "Explicit", "Simple", "Readability","Easy to share"]
    return list

def statement(benefit):
    print("The benifit is " + benefit )          


def benefits_of_functions():  
    benefits_list = benefits()
    for benefit in benefits_list:
        print(statement(benefit))


benefits_of_functions()

我得到了错误:

The benifit is Beautiful
None
The benifit is Explicit
None
The benifit is Simple
None
The benifit is Readability
None
The benifit is Easy to share
None

我无法理解“无”。您能帮我弄清楚为什么在输出中吗?

1 个答案:

答案 0 :(得分:2)

statement函数中返回而不是打印:

def benefits():
    list =  ["Beautiful", "Explicit", "Simple", "Readability","Easy to share"]
    return list

def statement(benefit):
    return "The benifit is " + benefit


def benefits_of_functions():  
    benefits_list = benefits()
    for benefit in benefits_list:
        print(statement(benefit))


benefits_of_functions()