Q:

Python function error string index out of range

So I am to find out what is wrong with this code and apparently I do not see anything wrong because I've just gotten into learning python.
Also when I run the function it will give me an error of "String Index out of range".
And I am to test if this does work.
So what seems to be wrong and how should I be testing them?
def is_reverse_of( st1, st2 ):

    """
    is_reverse_of : String String -> Boolean
    is_reverse_of tells if one string is the reverse of another.
    preconditions: st1 and st2 are character strings.
    """
    if len( st1 ) != len( st2 ):
        return False
    i = 0
    j = len( st2 )
    while j > 0:
        if st1[i] != st2[j]:
            return False
        else:
            i += 1
            j -= 1

    return True

This is what I got so far for the testing
def test_is_reverse_of():

    """
    a suite of pass/fail test cases to validate that is_reverse_of works.
    """
    # Complete this test function.
    st1 = str(input("Enter the string: "))
    st2 = str(input("Enter the string: "))

    is_reverse_of(st1, st2)

A:

The index is 0-based, so it is from 0 to len(str2) - 1, not len(str2). You can easily fix the problem simply by doing:
j = len( st2 ) - 1

By the way, you really just need one index, eitehr i or j, since the other one can be easily calculated:
def is_reverse_of( st1, st2 ):
    if len( st1 ) != len( st2 ):
        return False
    l = len(st1)    
    for i in range(0, l):
        if st1[i] != st2[l - 1 - i]:
            return False
    return True

