Python 12:13 Passing a range into a function


#1

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

print my_function(range(3))

both that or (range(0,3,1) work however the instructions are confusing
I guess by the answer needing to return [0,1,2] it means those indices,
not those numbers. Something about print showing the new value for the selected
indices would be very helpful.


#2

Moreover, if you try to comment out string with multiplication, like this:

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

print my_function(range(3))

You'll get numbers corresponding the instruction ([0, 1, 2]).

BUT, you also will get Oops, try again. It looks like you didn't pass a range consisting of 0, 1, 2 to my_function. message.

Really confusing exercise


#3

This will omit the Start and Step operators and simply use the Stop operator, which is "3"

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

print my_function(range(3))

#4

We are suppose to use a range() function that returns [0, 1, 2]
After using the my_function we created on the range, [0, 1, 2] will return [0, 2, 4]
Therefore nothing wrong there.


#5

Sometimes I wonder if this is actually a platform for beginners like myself. Code Academy gives you a set of instruction to follow. The instruction doesn't work as expected. Then, suddenly someone (probably very experienced programmer) on this platform comes up with a solution or formula a novice like myself would never have imagined. My question, is this really a platform for beginners? Why do we have these experienced programmers on this platform as well if this was a beginner forum? Now, it will only make sense to me if they're just here to help novice like myself or they work for Code Academy's support service

My point: It will make sense to create a forum for beginners to enable learning from the scratch easier for beginners


#6

I ran this code in idle and got a type error but in code academy it works:

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

print(my_function(range(3)))

Found out that the syntax has changed in python3, range is a generator object - it does not return a list. Convert it to a list before running.

print(my_function(list(range(3))))

works with python3