Printing out a list item by item in a function


Why is needed at all in this code? Why do I need to call the function with the argument n?

x = [3, 5, 7]

def print_list(x):
    for i in range(0, len(x)):
        print x[i]



If you use that exact code I would expect you to see the following error:

Traceback (most recent call last):
  File "python", line 7, in <module>
NameError: name 'n' is not defined


@albionsrefuge I did and there were no errors. It said way to go :smile:

n = [3, 5, 7]

def print_list(x):
for i in range(0, len(x)):
print x[i]


it also ran well when changed the list name to 'n'


It sounds like you are working on one of the exercises. If you tell me which one then I can have a closer look.

#5 Here you Go.



Here is what I get when I try your code in that exercise:


You should refresh the page between "submits" to clear out any variables from the previous try.


Oh. Yep it now returns the name error. Thank You. :slight_smile:


I do not know what I have done wrong here


Because of the way you have (not) indented your for loop, it has put your return outside of your function -- which is not allowed.


I am trying to complete the last instruction but I do not know how to represent it in code.


You don't need anything after line 7.

Your program won't return what you are planning though -- check carefully: when will your return trigger?


It keeps giving that error when I run it


Why do you think that your function is returning 0 when you feed it [0, 3, 6]?

If it is returning 0, that sounds like it might be returning after the first element in the list.

Step through your code slowly and think about what you are telling it to do at each line, especially once you get into your for loop.


I honestly do not realize what is wrong. The function provides iteration through the elements in the numbers list and sums it all, I am not seeing the error in my code. Does it have something to do with the 'n'?


for item in numbers: // It starts to loop through the elements
    result = result + item // It adds the first element
    return result // It exits the function

See the problem now?


I just had the same problem (after solving other problems)- the return has to be outside the for loop?


But now here is my full code:
n = [3, 5, 7]

def total(numbers):
result = 0
for i in numbers:
result = result + i
return result

Codecademy says its correct for the lesson, but is that the whole lesson? this part is confusing: "Create a function called total that adds up all the elements of an arbitrary list and returns that count, using the existing code as a hint. Use a for loop so it can be used for any size list."

But maybe its just a reiteration of the previous part of the assignment:

Create a function that returns the sum of a list of numbers.

  1. On line 3, define a function called total that accepts one argument called numbers. It will be a list.
  2. Inside the function, create a variable called result and set it to zero.
  3. Using one of the two methods above, iterate through the numbers list.
  4. For each number, add it to result.
  5. Finally, return result.


Regarding this exercise --- why do you have to have an indent for print x[i] and NO indent for print_list(n)? I would think that the print_list(n) would need to be "included" under the function by being indented? Thanks!


Because print x[i] prints each item in the function.
print_list(n) calls the function print_list for the list n.

When you put print_list(n) inside the the indented, it would be a function call of itself inside the function, and that wouldn't work ( I suppose, I'm still learning too).