Why my code dont work


#1



def reverse(text):
    i = len(text)
    final = ""
    while i >= 0:
        final += text[i]
        i -= 1
    else:
        return final.


#2

well, strings are zero indexed based (counting starts at one), but len() start counting at one. So for example hello would have a length of 5 and the following indexes: 0, 1, 2, 3 and 4.

you use length in the first iteration of your loop, but this out of range for the reason i just explained above


#3

thank you for your explanation, and i set:
i = len(x)-1
problem solved


#4

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