# Reverse w indexes

#1

I've read a few posts on here and cant find someone who has taken this approach. Where am i going wrong? I've seen several ways that are more efficient but I want to try and make this work. Thx!

``````def reverse(text):
reverse = ""
for a in text:
back = 0
front = 0
text[len(text)-back] = reverse[front]
back = back + 1
front = front + 1
return reverse``````

Then I tried shortening:

``````def reverse(text):
reverse = ""
for a in text:
back = 0
text[len(text)-back] = reverse[0]
back = back + 1
return reverse``````

#2

Why write this when you will not be using a anywhere in your code. If you haven't grasped the concept of for loops try to repeat the lesson on that.

Take a closer look at this line because because You are not doing what you intended to do. You doing something completely opposite.
Again try to review assignment operation.

trace out the code you have written an you will know what this will give you.

I suggest you insert print statements after every line in your code to know what the current value is at each step of the iteration.

Why don't you get some pointers from those posts. I have encountered a lot of posts with code that uses similar logic to your design.
That would be a goo idea since you bent on doing it your way but you need to go back an take an in-depth look at the lessons before this one.

Regards
@rydan

#3

Thanks rydan. I reviewed the relevant lessons and modified the code. The way I would describe what I'm doing is taking the first character of the string and building a new string (reverse) starting from the back and working forward to [0]. Does Python even allow you to place something at [>0] before there is a [0]?

``````def reverse(text):
reverse = ""
for a in text:
letter = text.find(a)
reverse_loc = len(text)-letter
reverse[reverse_loc] = a
return reverse``````

#4
`````` > reverse("Python!")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 7, in reverse
TypeError: 'str' object does not support item assignment``````

The line after the error is where the exception is raised. That means the error message refers to,

What happens if you write `reverse` as a list, then join the return value? Spoiler alert...

``````def reverse(text):
reverse = []
for a in text:
letter = text.find(a)
reverse_loc = len(text)-letter
reverse[reverse_loc] = a
return ''.join(reverse)``````

Still a problem...

`````` > reverse("Python!")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 7, in reverse
IndexError: list assignment index out of range``````

This should answer your earlier question about assigning to an element that does not yet exist. Solving this problem could be a challenge, but a worthwhile pursuit for the learning value.

Tried this in line 6...

``````reverse.insert(reverse_loc, a)

> reverse("Python!")
=> 'P!yntoh'``````

Still experimenting, as I hope you are.

Tried this on line 6...

``````reverse.insert(0, a)

> reverse("Python!")
=> '!nohtyP'``````

This elliminates the need for lines 4 and 5.

``````def reverse(text):
reverse = []
for a in text:
reverse.insert(0, a)
return ''.join(reverse)``````

#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.