Passing a range into a function


#1

My code seems to work even when the required output should be [0, 1, 2]. Can somebody explain to me please.

def my_function(x):
    for i in range(0, len(x)):
        x[i] = x[i] * 2
    return x

print my_function(range(3))

my output is

 [0, 2, 4]

#2

HI this line

x[i] = x[i] * 2

muliply the number inside the range(0, len(x)) by 2 so this

 [0, 2, 4]

is teh right output and not this one [0, 1, 2]


#3

@wizmarco but that is what the exercise needs


#4

but that is what the exercise needs

yes it's that..


#5

I took the "* 2" and got the requested result of [0, 1, 2], but still got an "OOPS...." error.
Leaving the multiplier in will get a result of [0, 2, 4] and this will be accepted as being correct.
Confusing!


#6

I tried various approaches, and ended up with similar solution. Looks like this is an oversight by Codecademy. They are looking for a returned list of [0, 2, 4], I guess.


#7

I guess too, the following code returns [0,2, 4] and let me run to the next lesson.
def my_function(x):
for i in range(0, len(x)):
x[i] = int(x[i] * 2)
return x
n = [0, 1, 2]
print (my_function(n))


#8

Hi,
What is asked is to return a vase 0,1,2
we can do that in several ways.
1> range(0,3): it will start from 0 to 2 returning 0,1,2
2> range(0,3,1): it will have the same results as above, starting from 0 and then incrementing each by 1.


#9

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