Author Topic: Another python question.  (Read 1700 times)

0 Members and 1 Guest are viewing this topic.

Offline Sk33ter

  • /dev/null
  • *
  • Posts: 9
  • Cookies: -4
    • View Profile
Another python question.
« on: April 09, 2012, 10:03:18 pm »
As I said before, I am learning python and have come across yet another exercise that I can not quite complete.

This is what it says:
In this exercise you'll use an existing function, and while adding your own create a fully functional program.
 1. 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"
 2. 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!"
 3. Run and see all the functions work together!


This is what they give you in the code box to start out with:
Code: [Select]
#Add your functions here (before the existing functions)

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()


This is what I have so far:
Code: [Select]
#Add your functions here (before the existing functions)
def list_benefits(a, b, c, d):
    return a, b, c, d
def name_the_benefits_of_functions():
    list_of_benefits = list_benefits("More organized code", "More readable code", "Easier code reuse", "Allowing programmers to share and connect code together")
    for benefit in list_of_benefits:
        def build_sentence(info):
            return list_benefits, "is a benefit of functions!"
        print build_sentence(benefit)


name_the_benefits_of_functions()


It runs, but this is the outcome:
Code: [Select]
(<function list_benefits at 0x17133e3cd09ecd08>, 'is a benefit of functions!')
(<function list_benefits at 0x17133e3cd09ecd08>, 'is a benefit of functions!')
(<function list_benefits at 0x17133e3cd09ecd08>, 'is a benefit of functions!')
(<function list_benefits at 0x17133e3cd09ecd08>, 'is a benefit of functions!')

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Another python question.
« Reply #1 on: April 10, 2012, 12:08:54 am »
The code is a mess, but line 8 is the faulty one. Replace it with:
Code: [Select]
return info+" is a benefit of functions!"Do you know why? you are calling the first function without any parameters and that function should not be called anyway, so you use the "info" parameter from the sub-function. Since you are returning multiple objects in the first function, it returns as a list and because you don't give any parameters to return, it returns itself - address in the memory where it is being held.

Since everything is valid, you don't get errors, so it's a logical error.
Yes python is fucked, but once you understand it - it becomes an amazing tool :D
« Last Edit: April 10, 2012, 12:14:14 am by Kulverstukas »