14. Passing a range into a function


#1

Hi there!
I really confused. I already did it, and everything is working, BUT I tried to check a code in Python IDLE:
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!

and it's not working:(((

Python shows this error:
x[i] = x[i] * 2
TypeError: 'range' object does not support item assignment

Could you help me please to figure out it?
I will appreciate.
Thanks.


#2

@methodjumper50685,
Try it with

def my_function(x):
    print("Input  : {0}".format(x))
    for k,v in enumerate(x, start=0):
        x[k] = v**2
    print("Output : {0}".format(x))
    print type(x)
    return x
    
print my_function(range(3))

http://stackoverflow.com/questions/3303608/how-to-enumerate-a-range-of-numbers-starting-at-1

Python

== guidance ==
http://legacy.python.org/dev/peps/pep-0008/
http://docs.python.org/2/reference/expressions.html#slicings
http://docs.python.org/2/library/functions.html#range


#3

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

print my_function( [0,1,2])