7. Reverse



Oops, try again. Your code looks a bit off--it threw a "string index out of range" error. Check the Hint if you need help!

I want use 'counter' as a index for so that the index will count back starting with the last index and append the index value to 'reverse_list". If I replace text[counter] with text[0] the code returns "PPPPPPP" as expected telling me the rest of the code works. I'm assuming you cant use a variable as a list index even if the variable is a number. So what I'm trying to figure out is to how to automatically change the index of the list automatically every time I loop through the code.

def reverse(text):
    counter = len(text)
    reverse_list = []
    while counter != 0:
        counter = counter - 1
    new_list = "".join(reverse_list)
    return new_list
print reverse


This should be a function call with an argument.

print reverse("Python!")

Much safer if we use an inequality, rather than a not equal.

while counter > 0

When you set counter, be sure to subtract 1.


Ok thanks I'll make those changes. Why is an inequality not safe?


An inequality IS safe because the value can never fall below (or rise above) the limit set.

The != is what is dangerous. What if the value is never zero or somehow skips around zero into the negative? In this particular case it is not a deal breaker, since the code will work. It's just risky to use that kind of condition in a loop.


I see now, we want to avoid putting ourselves in a situation that could create and endless loop. I still have the same problem regarding the counter however. I'm unable to append(text[counter]) and am not sure how to iterate through the list another way.


Your first step is a good start, but needs to be assigned to something:

text_list = list(text)

The next step will be to initialize counter

counter = len(text_list)

Recall that because of zero indexing there is no element at text_list[counter].

while counter > 0:
    counter -= 1

return "".join(text_list)


Great, it worked so my initial thought of using the counter as the index was correct :slight_smile: I'm still trying to work through why I needed to subtract 1 from counter before doing the .append. If I'm understanding it correctly (which i think i am) 0 indexing means that your first element of the index is at position 0. therefore index 0 = P and index 6 = ! in this example. When i performed len(text) i would have expected that to = 6 since we start at 0. I feel like I must be incorrect on this point since starting at 6 and - 1 would leave me at index 5. So does the len function start counting from 1 or would you get a length of 0 of you fed it an empty string?


len("Python!") == 7

I subtract 1 in the loop before attempting to access the list so when counter is 7, the first element we access is [6]. We wind down to 1, which gets 1 subtracted so the last element accessed is [0].

The length is literal. An empty string has length, 0. Consequently there are no accessible elements since one less would be negative.

In case you don't know, negative indexes start from the right.

>>> "Python!"[-1] == "!"


got it. thanks for your help


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