I’ve just started learning Python a few hours ago, and there seems to be a problem that I simply can’t seem to get.
They ask me to:
-
Add a function named list_benefits()- that returns the following list of strings: “More organized code”, “More readable code”, “Easier code reuse”, “Allowing programmers to share and connect code together”
-
Add a function named build_sentence(info) which receives a single argument containing a string and returns a sentence starting with the given string and ending with the string ” is a benefit of functions!”
-
Run and see all the functions work together!
I’ve googled this question, but all of them seem to be for previous versions of python, I was hoping for an updated way to do this.
Given Code:
def name_the_benefits_of_functions():
list_of_benefits = list_benefits()
for benefit in list_of_benefits:
print build_sentence(benefit)
name_the_benefits_of_functions()
Expected output:
More organized code is a benefit of functions!
More readable code is a benefit of functions!
Easier code reuse is a benefit of functions!
Allowing programmers to share and connect code together is a benefit of functions!
What I have tried:
def list_benefits():
benefits_list = ["More organized code", "More readable code", "Easier code reuse", "Allowing programmers to share and connect code together"]
return benefits_list
def build_sentence(benefit):
return "%s is a benefit of functions!" % list_benefits()
def name_the_benefits_of_functions():
list_of_benefits = list_benefits()
for benefit in list_of_benefits:
print(build_sentence(benefit))
name_the_benefits_of_functions()
Output:
['More organized code', 'More readable code', 'Easier code reuse', 'Allowing programmers to share and connect code together'] is a benefit of functions!
['More organized code', 'More readable code', 'Easier code reuse', 'Allowing programmers to share and connect code together'] is a benefit of functions!
['More organized code', 'More readable code', 'Easier code reuse', 'Allowing programmers to share and connect code together'] is a benefit of functions!
['More organized code', 'More readable code', 'Easier code reuse', 'Allowing programmers to share and connect code together'] is a benefit of functions!
Could anyone tell me what I’m doing wrong?
Each time you call the
build_sentence()function, you only want it to build a sentence using a single benefit, which you specify in itsbenefitargument.For each iteration of this loop:
a single benefit is passed to the
build_sentence()function, and that’s what you want to print.