Problem with the reverse excercise


#1

def reverse(text):
list_i=[]
for i in range(len(text)):
list_i.append(text[len(text)-i])
print list_i

this is my code for the reverse exercise but its showing a "string index out of range" error, can someone help me with this please


#2

lets say i want to reverse the following string:

"hello"

agree that the length is 5, and the indexes are: 0, 1, 2, 3 and 4?

so, for the first run of the loop, i is zero and the length is 5, so:

text[5-0]

we don't have an index 5, the highest index 4, that is why you get an error


#3

Thanks alot , its a stupid error


#4

no, its a difficult one. Hope you are now able to solve the problem yourself :slight_smile: , now that you know what the problem is. Otherwise, post an updated version of your code


#5

Thanks alot , i was able to solve it


#6

Nicely done :slight_smile: You did most of the work and thinking, i just had to give you a little nudge in the right direction :slight_smile:


#7

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