Help with exercise 7.7


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

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

error line 2: can't assign to function call

#2

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.


#3

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


#4

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?


#5
x = "yessir"
y = len(x)

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

Still getting the same thing, line 5

Putting it in a var didn’t work


#6

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


#7

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


#8

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

print [y, -1]

see? A list, no length


#9
y = int.len(x)

Up there


#10

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


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

With that code, it returns integers in a list


#12

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


#13

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?


#14

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

things don’t magically become an integer.


#16
x = "yessir"

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

reverse(x)

Still returns same error


#17

see documentation:

https://docs.python.org/2/library/functions.html#range

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


#18

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)

reverse(x)


#19
x = "yessir"

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

reverse(x)

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.


#20

When text is an iterable object such as a string,

len(text)

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

text[y]

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.


#21

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?