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

Dhruv Ghulati
Dhruv Ghulati
1,582 Points

Not sure how to create the code for add_list

What am I doing wrong here?

functions.py
def add_list(list):

  while True:

    if i>len(list):
      break

      for i in list:
        i=0
        return list[i]+list[i+1]
        i=i+1
    continue

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

5 Answers

Dhruv Ghulati
Dhruv Ghulati
1,582 Points

OK I worked it out - basically simpler than we both thought Brian!

def add_list(list):
  total=0
  for i in list:
    total=total+i
  return total
Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Very Nice. Much more straight-forward than your original approach.

Brian Finkelstein
Brian Finkelstein
1,767 Points

I think the issue is that every time the for loop goes through a cycle, it sets i=0.

The syntax for num in list: should take care of that for you, I think...

Brian Finkelstein
Brian Finkelstein
1,767 Points

Can someone help explain what I'm doing wrong here too?

I keep getting an out of index error but I thought if you use the for something in somethings syntax, it should always know not to go out of bounds.

'''python

def add_list(num_list): while True: sum=0 for item in num_list: if item > len(num_list): break else: sum = sum + num_list[item] print(sum) return(sum)

Call the function with a list

add_list([1, 2, 3]) '''

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

You both used while True. This is a great construct, but only needed when you want something to run forever (or until explicitly broken with break). You really only want this to run until you run out of things in the list.

Joshua Ridley Joshua Ridley
Joshua Ridley Joshua Ridley
470 Points

Kenneth,

I'm assuming when we type the code we do not know how long or small the list is going to be. How would I create a loop function that knows when to stop with out being explicitly told to?

I thought about asking the user to give the total number of items in the list as the first part of the function, then making a "for" loop that stops at that number, but that would require the input() function.

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Joshua Ridley Joshua Ridley you just keep going until you get to the end of the list.

for number in range(50):
    print(number*2)

for number in range(5000):
    print(number/2)

Both of these for loops will run until they run out of numbers. I didn't tell them how to count or anything.

def double(nums):
    for num in nums:
        print(num*2)

double(range(50))
double(range(5000))

This does the exact same thing but deals with a list (well, list-like object) it doesn't know anything about until it gets to the function.