Writing word backwards

indexing, python, reverse

Solution

There are a couple issues with your code, I pointed them out in the comments of this adjusted script:

def rev(h):
    counter=len(h) - 1 # indexes of h go from 0 to len(h) - 1
    reverse=""
    while counter>=0: # changed to >=0
        reverse+=h[counter]
        counter -= 1
    return reverse

h=input('word\n\n');
revers = rev(h) # put rev(h) after the definition of rev!
print(revers) # actually print the result
# deleted your last line

In addition, you don't need to terminate lines with `;` in python and you can write `counter=counter-1` as `counter -= 1`.

Problem

I know there are possibilities : sampleword[::-1] or ``` reverse(string) ``` but I wanted to write it by myself. I don't get why my code doesn't work. Could you help me? ``` h=input('word\n\n'); rev(h) def rev(h): counter=len(h); reverse=""; while counter>0: reverse+=h[counter]; counter=counter-1; return reverse #print (reverse); ? input(); ```

Original source