Reverse --I got my code to work once I altered it. Please help me understand why this works


#1



So have been working on reverse for a while. Once I altered my code it works, but I don't understand why.

Originally, I was thinking: for i in range(len(text)-1-i)
This didn't work. Instead this worked:


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


#2

range has 3 values:

range(start, stop, step)

you want to start at the end of the string (length string minus one), you have do to do minus one because strings are zero indexed based

then the stop value is also minus one, given the stop value is not included

and minus one for step, since you want to go from a higher to a lower value


#3

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