Code won't run as per instructions


#1

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

checked this against other answers in this topic and others say it works but for me all I get is 3.
I tried resetting, going back a lesson, logging out yada yada yada! and still it won't loop so is it wrong and are others wrong when they say it's right?

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

#2

Here is error code:

Oops, try again.
It looks like you either didn't call print_list(n) or there is something wrong because 5 wasn't printed.


#3

The 'return' statement ends the function. So that's why you only get the first value out of it. Change 'return' into 'print' and the function will work like you want. Also remove the 'print' on the last line after that, as your function no longer return anything.


#4

Hey codeninja thanks it worked just wanted to let you know last night I was up late had to drop out of convo for sleep but I posted again this morning. You been a big help also you explain things much better then others thank you.


#5

Haha, thank you, and I wouldn't worry about not answering right away, it's a forum, not a chat room after all.


#6

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

This meets the instruction list, runs fine, returns the expected results, but throws the error code;
Oops, try again.
double_list([0, 1]) returned [6, 10, 14] instead of [0, 2]

n = [3, 5, 7]
def double_list(x):   
    for i in range(0, len(x)):
        x[i] = x[i] * 2
    return n

print double_list(n)

So why is this happening?


#7

what does the error code mean?


#8

Well, if you look at the error message you see that the tester tried to call your function with the input [0,1] but got the response [6,10,14] instead of [0, 2]. Why it got [6,10,14] is not completely obvious. What [6, 10 , 14] is however is pretty obvious. That is the result from doubling the elements in the list 'n'.

What happens here is because you return 'n' instead of 'x' in your function. You then run it once before the tester gets it's turn, resulting in changing the contents of 'n' into [6,10,14]. The tester runs it's first test double_list([0,1]). It changes the object to [0,2] as it should, but instead of returning it you return 'n' once again.

(Short answer: return x instead of n)


#9

Great explanation this is helping me a lot. I got to go to relatives for dinner but will be back later tonight. Hope to see you on I probably will need help, I'm sure of it. But I think I am making great progress thanks to you.