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

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,143 Points

Stuck on this one....Python Basic Stage 5

I got here two months ago, remember solving the first part and then my second response invalidated my first response, and I gave up. I am back to take another run at it. Sadly, I can't get the first part right anymore. Thanks in advance.

add_list = []

new_num = raw_input

def ask_num(raw_input):
    print("Enter numbers to add.")
    print("Enter DONE to stop inputting numbers")

def add_list():
for item in add_list:
        add_list.sum(new_num)

while True: 
    new_num = input(">")
    if new_num == 'DONE':
        break
    print "The sum of [] is ", add_list()```

Hi Nancy,

You have posted the same question an hour ago here: https://teamtreehouse.com/forum/stuck-on-code-challenge-python

If my answer didn't make sense to you then it's best to post back and ask for further clarification rather than double post your question.

Nancy Melucci
Nancy Melucci
Courses Plus Student 36,143 Points

I am sorry. I took your original comment as meaning I needed to repost in the correct location for those questions. Your answer did make sense to me. I am sorry for the trouble. If you want me to delete this (or if you want to do that) I am fine with it. Let me know.

I'm sorry for the confusion. I meant that if you post a new question in the future for a different challenge it will be easiest for you to "ask a question" from within the challenge.

I can see that you've done that here. :)

Let's try again on this one.

Nancy Melucci
Nancy Melucci
Courses Plus Student 36,143 Points

My idiocy is reflected here. I am beginning to feel like I have a Python block. I should calm down and keep at it. Thanks for your patience.

That was my fault. Don't blame yourself. I've added an answer here and took a different approach.

If you're still stuck, then post what you currently have. Make sure to remove all that extra code the challenge isn't asking for.

2 Answers

I can see that you've changed your code slightly from the previous question but it still contains a lot of code not called for in the challenge. You don't have to get user input or have a while loop. You only need the add_list function.

I'll put comments in the function and see if you can replace each comment with a line of code.

def add_list(my_list):
  #Initialize a total variable to 0

  #Create a for loop that will iterate over each item in my_list
    #Add the current number from the list to total

  #Finally return the total

See if you can replace the 4 comments with 4 lines of code that do what the comment says. Note the indentation levels.

I think you'll learn more if you can figure it out for yourself vs. seeing the answer.

See if that helps you.

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,143 Points

RIght...here's what I posted in the other place. It works but I think that I am asking Python to add the indices not the actual numbers. How do I make it know that I want the numbers added?

my_list = [1,2,3]

def add_list(my_list, total = 0):
  for i in range (0, len(my_list)+1):
      total = total + i
  print "The sum of", my_list, " is ", total

add_list(my_list)```

Alright, you're getting a lot closer!

Initializing the total variable should be the 1st line in your function, not a second argument to the function.

Second, you're correct. You are adding indices to total.

To get the actual numbers you want to iterate over the list itself for i in my_list:

Each time through the loop, i will be one of the numbers in the list.

Third, you want to return the total rather than print it.

I don't think it will hurt to create a list and then call your function but it's not strictly required for the challenge.