14. Code not working for Passing range into a function


Oops, try again. Your code looks a bit off. Check the Hint if you need help! Your code threw the following error: range() integer end argument expected, got list.
Traceback (most recent call last):
File "python", line 6, in
TypeError: range() integer end argument expected, got list.

it to work...

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

print my_function(range([0,1,2]))


this is your problem.
The first number should be where you start, the second where you stop, and the third where you step. (increment.)
Because you want your first number to be zero, you want to start at 0.
The second number you need to output is two, so think about what times two equals two.
1 * 2 = 2. This means that you need to increment by 1, so your last number to pass the range would be one.
The last number you want is where the function should stop. Because the output returns 3 numbers, and you're starting at zero and incrementing by one, you need to stop at three.
Simply, the lline of code you need is:

print my_function(range(0,3,1))


I understand the range concept , but don't you think the output they are expecting in instruction is different from the output w hich is shown correct by editor .
They are expecting range to be [ 0 ,1 ,2] and the output after passing range (0 , 3 , 1) is [0 , 2 ,4]


According to the instructions you are just supposed to use the range function not call the function you defined


I am not satisfied although its not a big issue .It is mention that "".......replace with a range() that return a list containing [0,1,2]


well, range() is a function call to a built in function.

so your range() should return a list of [0,1,2], and it does, then you pass this into my_function to double it, but the exercise is talking about what the range function returns, not my_function


Now understood.


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