Error: body of function containing 'n'


#1

n = [3, 5, 7]

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

I keep getting error "Oops, try again. The body of your function should not contain any references to 'n'". I think my function doesn't contain references to 'n'? Thanks in advance for any help.


#2

Hi @karaeska ,

First, modify the function definition to use indexing and len as exemplified by the default code.

Then, though it may seem a little tangential to the message that you received, remove this loop from your code ...

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

#3

Hi, @appylpye!

Okay, thanks for help! I'm gonna try it out later, however, I have to point out that the error says that the body of your function shall not have any references to n and I assume the body is this thing with def name(x), so removing the sample loop as a solution didn't really cross my mind :frowning: But I suppose that it's just like real life issues — sometimes the solution to them is not really the most logical one.


#4

Yes, @karaeska , you are right that the message does not adequately describe the problem.

What you should do is modify your function so that is does something similar to the original loop, then remove the original loop. When you are done, the function will have references to x instead of n.


#5

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