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

Unsupported operand types RE: function code challenge

I keep getting errors here when I submit my code. I was able to generate the sentence string and the total as part of the exercise using an IDE called Pythonista.

I thought the error occurs on line 35. I also tried assigning variables in place of str(new_list) and str(total) earlier and received errors. I'm not sure if the error is where I think it is or somewhere else in the code.

functions.py
# 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.

item_list = [1,2,3]
new_list = []
global sentence

def add_list(item):
  global tot
  i = item  
  sentence2 = str()

  if i == 1: #if block totals list
    tot = 0 
  else:
    pass
  tot = tot + i 
  sentence2 = summarize(i) #passes string from summarize function

  if i == 3: #if block stops function prints sentence string and returns total
    print (sentence2)
    num = tot
    return num

def summarize(x):
  total = 0
  sentence = str()
  new_list.append(x)

  if x == 3: #stops the function, sums list values, creates sentence string
    for z in range(len(new_list)):
      y = new_list[z]
      total = total + y
    sentence = "The sum of "+str(new_list)+" is "+str(total)+"."
    return sentence

for item in item_list: #loop passes one value at a time for list
  if item == 3:
    t = add_list(item)
    print (t) #prints total returned from function
  else:
    add_list(item)

1 Answer

Sorry, but your code is far more complex than it needs to be, and is quite difficult to debug.

I'd suggest going back through the stages you've already completed to get you back on track.

Specifically Stage 2 and the video String Formatting and Stage 4 and the video Shopping List Project, which covers for loops.

You shouldn't need to deal with the global keyword as I don't think that's covered in this course. You essentially just need one function to loop through a list and add the values together, and the other to format a string using values provided and calculated from the first function.

I basically misread the assignment. I was being too literal. Instead of trying to pass the whole list into the function through one variable, I passed each item within the list to meet the one argument rule. This assumption made the project much harder than it had to be.