Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

Mohsen Qaddoura
Mohsen Qaddoura
22,237 Points

Challenge Task step 2 None error

Here is my code:

def add_list(lst): total = 0 for item in lst: total +=item return total

def summarize(lst): print('The sum of {} is {}.'.format(str(lst), add_list(lst)))

The error is output is 'None' return instead of the required one.

2 Answers

You can make the add_list part easier:

def add_list(lst): return sum(lst)

def summarize(lst): return ('The sum of {} is {}.'.format(lst, add_list(lst)))

Mohsen Qaddoura
Mohsen Qaddoura
22,237 Points

Thank you. I'm finished with this basic Python course now. I need more advanced one, I do not know where to find a free one :)

Mohsen Qaddoura You get output 'None'. Because you are printing in your summarize() function instead return. Function generally return 'None'. You can override that by return statement.

Challenge 2 they are asking you to make a function summarize() that takes in list and return ""The sum of X is Y.", replacing "X" with the string version of the list and "Y" with the sum total of the list."

So you should return in summarize(). Please code below for more clarification.

def add_list(pass_list):
  total = 0
  for item in pass_list:
      total += item
  return total

def summarize(lst): 
    return('The sum of {} is {}.'.format(str(lst), add_list(lst)))

I hope it helps

Great! You can check Edx, Coursea and udacity (not free).