The following are links to additional questions that our community has asked about this exercise:
This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
Currently there have not been enough questions asked and answered about this exercise to populate this FAQ section.
This FAQ is built and maintained by you, the Codecademy community – help yourself and other learners like you by contributing!
Not seeing your question? It may still have been asked before – try () in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post ().
I was doing a basic mistake but I rectified it soon after I clicked the solution button. It gave me the following code:
def my_function(x):
for i in range(0, len(x)):
x[i] = x[i]
return x
print my_function(range(3))
In 3rd line, it is given : x[i] = x[i], but it doesn’t quite make sense. When I tried running the same code in Python 3 (with some syntax correction), it showed the following error:
def my_function(x):
for i in range(0, len(x)):
x[i] = x[i]
return x
print (my_function(range(3))
TypeError: 'range' object does not support item assignment
Thanks for the thread and thanks @mtf for the explanation. I was doing the same exercise of Python 2 but using an IDE that runs Python 3 on my own computer. I had the same error and could not figure out why until I checked here.
For my own test code, I used 2 ways to compare:
def my_function(x):
for i in range(0, len(x)):
x[i] = x[i]
return x
list_1 = [0, 1, 2]
print("Using List as x")
print(my_function(list_1))
list_2 = range(3)
print("Using Range as x")
print(my_function(list_2))
I could see that list_1 work but not the list_2 i.e. range() does not behave like a list. mtf’s explanation makes perfect sense why that is.