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

Mark Casavantes
PLUS
Mark Casavantes
Courses Plus Student 13,401 Points

TabError: elif add_list == 'HELP':

Where am I going wrong. I do not see it.

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.

# Make a function named add_list that takes a list. 
# The function should then add all of the items in the list together and return the total. 
# Assume the list contains only numbers. 
# You'll probably want to use a for loop. 
# You will not need to use input().

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

add_list = []

def show_help():
    print ("What are your numbers you wish to add? ")
    print ("Enter 'DONE' to stop.")
    print ("Enter 'HELP' for this help.")
    print ("Enter 'SHOW' to see your current list")

def add_list():
    num_list.append(items)
    print("Added! List has {} items.".format(len(num_list)))

def show_list():
    print ("Here is your list")
    print(add_list)

show_help()

`while True:
    if add_list == 'DONE':
            break
            add_list(num_list)
            continue       
        elif add_list == 'HELP':
                show_help()
                continue   
            elif add_list == 'SHOW':
                show_list()
                continue`
Michael Pastran
Michael Pastran
4,727 Points

okay so are you stuck in the first part? of the challenge because your approaching it wrong. so lists are iterables, which means that we can go through them one by one. so what we want to do is for example if we have the list = [1, 2, 3] we want to loop over the first item in the list and add it to a variable. lets says that variable is results = 0. after the first loop results is going to be results = 1, then on the second loop we are going to add the current value of results PLUS the current item being looped over. in this case its 2. so now results = 3, and then we do it a third time. results = 6 after the 3rd loop.

def add_list(list):
  results = 0
  for number in list:
    results += int(number)
  return results

also notice how i wrote int(number) thats just in case the original list has numbers in their string version. we want to convert them to int so they can be added.

let me know if thats what you needed or if your stuck on the second part of the challenge. also let me know if its a bit confusing

Mark Casavantes
Mark Casavantes
Courses Plus Student 13,401 Points

Thank you Michael,

I appreciate you setting me straight on the beginning of my program.

I still get errors at the bottom.

I also am curious about +=. I have seen == but not +=. Please fill me in as to what this does.

I am curious about how to debug my own errors.

Thank you,

Mark

3 Answers

Mark Casavantes
PLUS
Mark Casavantes
Courses Plus Student 13,401 Points

I regrouped and started over and obtained this solution. For a while I thought I was going to be stuck.

def add_list(num_list):

    total = 0

    for i in num_list:

        total = total + i

    return total

print(add_list([1,2,3]))
Michael Pastran
Michael Pastran
4,727 Points

yeah no problem. and okay so for the " += ". im assuming you know the difference between " = " and " ==".. one is assignement "=" and the other means 'equal to' "==". so in essence what is happening with " += " is that we are adding and then assigning. in reality this is how it looks " results = results + int(number) " you could write it like this if it makes it easier to understand but youll get used to the logic. you could also do " -= " which means we subtract first and then assign.. then you could also do " =+ " or " =-" in these case we are assigning and then adding, subtracting.

by errors at the bottom? are you referring to you code?

Michael Pastran
Michael Pastran
4,727 Points

btw mark, if you wanted to do it the way you were doing it, you would have to change a couple things around. i copied your code and modified it so that it would work. this is my version:

current_list = []
def show_help():
    print ("What are your numbers you wish to add? ")
    print ("Enter 'DONE' to stop.")
    print ("Enter 'HELP' for this help.")
    print ("Enter 'SHOW' to see your current list")
    print ("Enter 'ADD' to add all the numbers up")

def add_numbers(current_list):
  results = 0
  for number in current_list:
    results += int(number)
  print (results)
  return results

def show_list():
    print ("Here is your list: {}". format(current_list))

show_help()

while True:
    user_input = input(">>>: ").lower()
    if user_input == 'done':
        break
        show_list()
        continue       
    elif user_input == 'help':
        show_help()
        continue   
    elif user_input == "show":
        show_list()
        continue
    elif user_input == "add":
        add_numbers(current_list)
    else:
        current_list.append(user_input)
        show_list()
        continue
    ```

the main difference is that you write a way for the user to put his numbers or the choice he wants (help, show...ect)
Mark Casavantes
Mark Casavantes
Courses Plus Student 13,401 Points

Thank you Michael,

I have been working on the code and I still get errors. I am stuck. I have been experimenting. Also, is there a way that you can watch videos without all your work disappearing? I do not know what I am missing.

Also, I notice you use lower case for done, help, show, and add. I assume this means that capital and lower case letters can be interchanged.

I hope I work out my error(s) soon.

Thank you,

Mark