# I do not get how to pass a range into a function or how the start, stop, step thing works. def my_function(x): for i in range(0, len(x)): x[i] = x[i] * 2 return x print my_function(range(3)) # Add your range between the parentheses! # This is my code and I was just trying a bunch of numbers to attempt to learn by trial and error but I put in range(3) and for some reason that worked. [0, 2, 4] None # This is what my code returned and it worked although the code was supposed to return [0, 1, 2]. Some help and explanation would be much appreciated.
When you type
range(3) you are defining a list of integers between 0 and 2. i.e.
range(start, stop, step) you can define where you want your list to start, end and the size of the step between terms. If you don't include a value for the step, then the list assumes a default of 1. Here are some examples below:
You can also have non-integer values for the step:
What your function is doing is the following:
- Function takes an list
xas an argument.
- Function loops over the length of the list and resets
x[i]which is the
ith term in
xto a value of
- The function returns the new list in which now each term is double that of the one you initially passed.
I hope this helps you have an idea of the range you are supposed to pass to the function in order to return a list of
Thank you very much it makes sense now.
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.