Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (Retired) Putting the "Fun" Back in "Function" Functions

I don't get this at all :/

What am I supposed to do here? I have no clue.

Which question are you on?

2 Answers

hie marwanbakr. okay let me take you through the challenge step by step. task 1

  1. am sure by now you know how to define a function so according to the first statement you need to define a function called add_list that takes a list of numbers, which is def add_list(a_list):. note well I used the parameter a_list, using list my give you problems since list is a built in function that already exists in python. don't forget to put :
  2. now we create the variable total that is going to hold the sum of the numbers in the list. we initialize our variable total by setting it to zero. total = 0
  3. thirdly we use a for loop to add the numbers in our a_list, for number in a_list:
  4. then add that number to our variable total. this loop iterates through our list adding every number in a_list to the variable total. don't forget to indent this line so that it goes inside the for loop, total = total + number.
  5. finally we return our total. note well, you should check your indentation very well. be carefull not to put your return inside the for loop. your return should be inline with the for for the for loop.

def add_list(a_list):

total = 0

for number in a_list:

  total = total + number

return total

task 2

  1. we go through all the steps in task 1 again but now we return the string version of it i.e the sentence. we use the .format function to replace the curly braces with values we want. return "the sum of {} is {}".format(a_list,total) try following this code

def summarize(a_list):

 total = 0

 for number in a_list:

     total = total + number

 return "The sum of {} is {}.". format(a_list,total)

NB: pay special attention to indentation, parenthesis() and the colon: I hope this helps. get back to me if you have any problems.

hie marwanbakr. okay let me take you through the challenge step by step.

task 1

  1. am sure by now you know how to define a function so according to the first statement you need to define a function called add_list that takes a list of numbers, which is def add_list(a_list):. note well I used the parameter a_list, using list my give you problems since list is a built in function that already exists in python. don't forget to put :
  2. now we create the variable total that is going to hold the sum of the numbers in the list. we initialize our variable total by setting it to zero. total = 0
  3. thirdly we use a for loop to add the numbers in our a_list, for number in a_list:
  4. then add that number to our variable total. this loop iterates through our list adding every number in a_list to the variable total. don't forget to indent this line so that it goes inside the for loop, total = total + number.
  5. finally we return our total. note well, you should check your indentation very well. be carefull not to put your return inside the for loop. your return should be inline with the for for the for loop.

def add_list(a_list):

total = 0

for number in a_list:

      total = total + number

return total

task 2

  1. we go through all the steps in task 1 again but now we return the string version of it i.e the sentence. we use the .format function to replace the curly braces with values we want. return "the sum of {} is {}".format(a_list,total) try following this code

def summarize(a_list):

total = 0

for number in a_list:

  total = total + number

return "The sum of {} is {}.". format(a_list,total)

NB: pay special attention to indentation, parenthesis() and the colon: I hope this helps. get back to me if you have any problems.

don't forget to upvote

don't forget to upvote