Need Help with Python exercise

431 views
Skip to first unread message

James Hall

unread,
Dec 21, 2012, 11:01:01 AM12/21/12
to python-g...@googlegroups.com
Hi,

I'm just working on learning python and I've extremely new to programming. I'm going through the exercises at learnpython.org and I'm stuck at the fuctions: http://www.learnpython.org/page/Functions#

Any help with what the code should look like for this one?

Thanks!

Shahab Shahid

unread,
Dec 21, 2012, 12:10:27 PM12/21/12
to python-g...@googlegroups.com
The code for this exercise should look like this:-

# Modify this function to return a list of strings as defined above
def list_benefits():
    list=["More organized code""More readable code""Easier code reuse""Allowing programmers to share and connect code together"]
    return list
    pass

# Modify this function to concatenate to each benefit - " is a benefit of functions!"
def build_sentences(benefit):
    return benefit+" is a benefit of functions!"
    pass

def name_the_benefits_of_functions():
    list_of_benefits list_benefits()
    for benefit in list_of_benefits:
        print build_sentences(benefit)

name_the_benefits_of_functions()
Shahab Shahid Khawaja

James Hall

unread,
Dec 21, 2012, 12:33:31 PM12/21/12
to python-g...@googlegroups.com
Awesome! You rock, thanks for the help!

I'm still working on getting myself into the programming mindset. This is my first stab at learning programming. I made it through the previous exercises but this one was tough for me.

James Hall

unread,
Dec 21, 2012, 1:58:21 PM12/21/12
to python-g...@googlegroups.com
Can you explain why this is needed:

list_of_benefits list_benefits()

Is it because "for" can't use a function?

Shahab Shahid

unread,
Dec 22, 2012, 1:41:15 AM12/22/12
to python-g...@googlegroups.com
The reason I came across is that list_of_benefits() returns a list of values which can be stored in a variable by assigning the result to that variable. If we were to use list_benefits() directly it would imply that this was a variable of type list, whereas it is actually a function type. Type mismatching would be the short answer to your query. 
Shahab Shahid Khawaja

Robert Mandić

unread,
Dec 22, 2012, 4:10:12 AM12/22/12
to python-g...@googlegroups.com

You can use it directly in a for loop without assigning it to a variable. If a function returns an iterable (list, dict, set).

for var in my_func():
    # do stuff with var
    
In your case:
for benefit in list_benefits():
    # do stuff with benefit

Lost Samba

unread,
Mar 10, 2013, 5:25:07 PM3/10/13
to python-g...@googlegroups.com
Thanks!!!
Reply all
Reply to author
Forward
0 new messages