Why does it say n should not be used in the body of my function when it isn’t?

Question

Why does it say n should not be used in the body of my function when it isn’t?

Answer

This seems to be an error present in this exercise at the time of writing. If you leave the initial for loop code as it is and write your print_list function after it, it marks your code incorrect even if it is the right solution.
To work around this, delete the initial for loop and the print statement inside of it and try running your code again.
If that doesn’t work, check to make sure you are iterating using x as your list you’re checking and printing values from, and that you are using your function by calling print_list(n) at the end, outside of any functions (unindented).

5 Likes

In this exercise, after running the function print_list(n) in the console I got the values 3,5, and 7 printed individually, but I also got “None” printed on the last line. What does it mean?The program runs smoothly and does not show any error message, is this a Semantic Type of error ?

2 Likes

I have the same question about why None gets printed after the each number.

1 Like

I wrote the program differently, but it still works. Why?

n = [3, 5, 7]
def print_list(x):
for x in range(0, len(n)):
print n[x]

1 Like

it doesn’t work, look:

n = [3, 5, 7]
def print_list(x):
 for x in range(0, len(n)):
  print n[x]

print print_list([2, 4, 6])

it should print:

2
4
6

which is not happening.

the function should be capable of printing any list you provide as argument at function call

3 Likes

Got it. Thanks for your help!

1 Like

That’s because function print_list(n) returns None. It prints the whole list and returns None.

1 Like

why we can’t use"return" to end the function? I tried return x[i] instead of print x[i], but when i recalled the function with n, nothing is shown.

1 Like

return is not the same as print, you can print the returned result by putting a print statement before the function call

2 Likes

but can you why nothing is shown in the blow screen grab please?

and why the returned result seems to be the first element of the list instead of each element?

1 Like

because you don’t print anything

because that is what return does? returns mean literally that, handing something back

its kind of like when making an exam, lets say the exam has 3 questions (just like the loop has 3 iterations), you can perfectly return the exam to the teacher after having only filled in the answer to the first question. Same with the loop

4 Likes

Thanks for your help!

1 Like

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

The thing is, you’re supposed to keep the for loop INSIDE your function, and when you do that, change all the "n"s in the loop from “n” to “x”. This worked for me, and I had no problems.

1 Like

Did you remember to use “return”? that may be it

I also had this problem, it cleared once I removed the previously provided for loop.

def print_list(x):
print x
for i in range((0,len(x)):
returnx[i}
print_list(n)
That should fix your problem with none getting return at the end.

But this creates other problems, this returns the first number, signaling the function is done.

I just ran into the same problem here. When you take out the len() function and the associated for loop, a message pops up that says the lesson was modified to use the len() function. To get around this, just create a variable and set it to equal len(whatever item you have in your for loop).
My code looked like this:

n = [3, 5, 7]

def print_list(x):
for item in x:
lenth = len(x)
print item
print_list(n)

This way, there is a len() function in the code to satisfy the requirement for the lesson, but it doesn’t change the output.

When I type this it crashes my tab? Why is that. It just keeps looping.

n = [3, 5, 7]

def print_list(x):

  for i in range(0, len(x)):

    print x[i]

 #space# print_list(n)

When I do this, without a space it is fine.

n = [3, 5, 7]

def print_list(x):

  for i in range(0, len(x)):

    print x[i]

#no space# print_list(n)

The only difference is a an indent/no indent before the print_list(n)

In the first example, the function call is within the function. So the function will endlessly invoke itself, which is known as recursion.

2 Likes