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

"Fun with Functions" (Python Basic Stage 5) - problem implementing within menu

Hello, when I try to implement the Python Basic Stage 5 fun with functions challenge in my larger program it throws an exception. I've commented where the problem appears, and I'm not sure why it is doing this. Any help would be appreciated.

shopping_list = []

def show_help():
    print("What should we pick up at the store?")
    print("Enter DONE to stop. Enter HELP for help."""
          """Enter SHOW to see your current list. Enter ADD to see contents""")

def add_to_list(item):
    shopping_list.append(item)
    print("Added! List has {} items".format(len(shopping_list)))

def show_list():
    print("Here's your list:")
    for item in shopping_list:
        print(item)

def add_list(list_num):
    total = 0
    for num in list_num:
        total += num
    return total

def summarize(list_num):
  sum = add_list(list_num)
  print("The sum of {} is {}.".format(list_num, sum))

show_help()

while True:
    new_item = input("> ")
    if new_item == "DONE":
        break
    elif new_item == "done":
        break
    elif new_item == "HELP":
        show_help()
        continue
    elif new_item == "SHOW":
        show_list()
        continue
    elif new_item == "ADD":
        summarize(shopping_list) #unsupported operand type(s) for +=: 'int' and 'str'
        continue

    add_to_list(new_item)
    continue

show_list()

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

This is for passing the code challenge? You don't need print() or that while loop. You just need two functions, add_list and summarize. The first one adds all of the items in the list together. The second returns a string about the list. Scale way back and see if you can pass it.

As for your exception, if you're running this locally, every time you get input(), you're getting a string. Your add_list function is then trying to add those strings to the number 0, which Python can't do.

Hi Kenneth, I passed the online challenge and was trying to extend it locally. Could you perhaps give me a pointer as to how I'd integrate the add_list function locally so it doesn't create that exception? I've been trying to figure it out but have struggled a bit. Thanks.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Jack Clark Just don't make them be strings :) You can put int() around the input() call or convert the variable afterwards.

Really, though, that doesn't make a lot of sense since you're adding names of things you want to buy to a list.