Printing out a list item by item in a function does not accept valid answers


#1

https://www.codecademy.com/courses/learn-python/lessons/lists-and-functions/exercises/printing-out-a-list-item-by-item-in-a-function?action=lesson_resume

<In what way does your code behave incorrectly? Include ALL error messages.>
“This lesson has been recently modified to use ‘len’. Please hit reset and try again”

<What do you expect to happen instead?>
The lesson should pass with this code:

 n = [3, 5, 7]

 def print_list(x):
    for i in x:
       print i

 print_list(n)

When I click “Get Code” it gives me this code which has the same error and does not pass the lesson either:

n = [3, 5, 7]

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

print_list(n)

This makes no sense. Both answers should be accepted however neither are.


#2

Weird, both of these solutions work for me and allow me to pass. Try resubmitting (make sure you have properly indented your code).


#3

I did, both solutions fail for me.


#4

After further investigation, It appears Codecademy’s SCT is very picky about syntax in this exercise. I agree that both the answers provided should be accepted, and I will report this as a bug, for now, reset the exercise and follow what you are told to do in the hint…

Hint:

You can simply place the code on lines 3 - 4 inside a function definition. Make sure to indent properly! And you’ll have to change the n to x

n = [3, 5, 7]

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

print_list(n);

I apologise for any inconvenience.


#5

The posted code has indents that are not what are expected. Should be four spaces per block level indent.

Aside

When we are just printing (and order is not an issue) then the look-up approach is the way to go…

for i in x:
    print i

If order does matter, then we need the index approach…

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

#6

@mtf Even if four spaces of indent are given like this:

n = [3, 5, 7]

def print_list(x):
    for i in x:
        print i

print_list(n);

it is still not accepted.

I have tested this quite thoroughly and it appears to only accept the following function:

n = [3, 5, 7]

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

print_list(n);

#7

That’s the one that passed for me. Must be a pattern match in the SCT.


#8

Looking at the SCT, it’s not checking the indentation, but the editor is mixing spaces and tabs if you’re not careful. As long as my code uses only spaces or tabs, it passes without error. Tested with 2 spaces, 4 spaces, 8 spaces, and tabs.

(here because I was invited to the topic)


#9

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