What's wrong with my function? (Printing out a list item by item in a function)


#1



https://www.codecademy.com/courses/python-beginner-nzzVa/3/1?curriculum_id=4f89dab3d788890003000096#


Did you create a function called print_list?

File "python", line 5
print n[i]
^
IndentationError: expected an indented block

I DID CREATE A FUNCTION!!

n = [3, 5, 7]

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


#2

print n[i]

1.Indentation !
2.n ? (What is x doing there then?)


#3

They said to define a function called print_list that has one argument called x.


#4

you're not using the x but n?Why?


#5

Ok, I used this code:

n = [3, 5, 7]

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

print_list(n)

It said the list index is out of range. What did I do wrong?


#6

len(n) ,still n there?
did you indented your code after you defined loop?


#7

I did, but I still don't know where to ident at.


#8

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

4 spaces


#9

I got it now! thank you!


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.