Help with exercise 7.7

def reverse(text):
  for range([0, len(text), -1]) in text:
    print str(text)
x = "yessir"    

I don’t know why it says the function isn’t defined.

error line 2: can't assign to function call


is that the error codecademy gives? The relevant error is here:

error line 2: can't assign to function call

so lets look at line 2:

for range([0, len(text), -1]) in text:

can’t assign to function call, okay so where do we have a function call on this line? range(), exactly. What arguments does range expect? Clearly not a list.


I don’t know how I can assign it before I call it. Because it seems like the only way to define properly is to include the length of the string in the final range


range is a built-in function (you can check it out in the docs), which can take up to three arguments:

range(start, stop, step)

which should be integers, so not sure why you use a list in combination with range. Which of the two do you want to use?

x = "yessir"
y = len(x)

def reverse(text):
  for range([y, -1]) in text:
    print str(text)

Still getting the same thing, line 5

Putting it in a var didn’t work


you are using the range function wrong, range() expects integers as arguments, not a list. I already recommended you to check the documentation for the range function


length of a string converted to integer, is not an integer?


where do you get the length of the string? Look:

print [y, -1]

see? A list, no length

y = int.len(x)

Up there


that is fine, but you are still not using range() correctly.

y = "hello"
x = int(len(y))
print [x, -1]

With that code, it returns integers in a list


yes, but range expects integers as arguments, not integers in a list as arguments.


Hm, weird. I thought that if the length was already taken, it would just become integer, especially if it’s assigned to a var that way - but I guess the list aspect of the string still remains somehow?


the length already taken? What do you mean by that?

things don’t magically become an integer.

x = "yessir"

def reverse(text):
  for range([0, -1]) in text:
    print str(text)


Still returns same error


see documentation:

you feed integers to range, then range produces a list.


That’s what I did though, I fed integers (0, -1) to range

x = "yessir"

def reverse(text):
  for range(0, 0, -1) in text:
    print str(text)


x = "yessir"

def reverse(text):
  text[y] = int(len(text))
  for range(text[y], -1, -1) in text:
    print str(y)


Tried this too.

I’m confused, and the solution button has disappeared. So am I never going to learn this? The docs aren’t telling me anything.


When text is an iterable object such as a string,


is an integer, so it is not necessary to cast it as an int.


y is undefined. This whole line can be removed.

text[y] = int(len(text))

You’re on the right track though.

range(len(text), -1, -1)

The sequence that results will be,

[6, 5, 4, 3, 2, 1, 0]

but we can see there is in character at text[6] so the range needs to be adjusted,

range(len(text) -1, -1, -1)

Now we need to assign an iterator to hold the character at each position in the string.

for y in range(len(text) -1, -1, -1):

The exercise expects us to return a string, so that will need to be assembled in the loop.


What does the error ‘int object has no attribute get_item’ mean? Why does it need ‘get_item’ when I try to print text[y] within loop?