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

problem with a exercise "make a function named summarize"

I got throw the first challenge. But the problem is that while I'm doing the second challenge in a code I mess something up and I don't get what it is... It gives back an error "Oops! It looks like Task 1 is no longer passing." Code:

# add_list([1, 2, 3]) should return 6
# summarize([1, 2, 3]) should return "The sum of [1, 2, 3] is 6."
# Note: both functions will only take *one* argument each.
def add_list(list):
  total = 0
  for i in list:
    total +=i
  return total

x = str(list)
y = total
def summarize(list):
  return("The sum of ", x " is ", y.)

1 Answer

The problem is that you have quite a few formatting issues and seem to be misunderstanding the nature of scope when defining functions!

Your summarize function, as you have defined, takes one argument, list. However, you are trying to operate on the variables x and y, which you've defined outside the function, in the "global" scope. Python functions will not check this global scope, and instead rely on the arguments you've passed into it and any you've defined within it - this is the function's "local" scope. So your function has no idea what x and y are, nor can the Python set the value of y because it has no idea what total is!

So try again, and remember that a function can call another function.

You'll also need to read on how to properly format and concatenate strings. The return line of summarize is incorrect syntax.

Thank you for the answer and feedback!