7. Reverse


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/1?curriculum_id=4f89dab3d788890003000096#



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):
    list(text)
    counter = len(text)
    reverse_list = []
    while counter != 0:
        reverse_list.append(text[counter])
        counter = counter - 1
    new_list = "".join(reverse_list)
    return new_list
print reverse


#2

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.


#3

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


#4

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.


#5

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.


#6

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
    reverse_list.append(text_list[counter])

return "".join(text_list)

#7

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?
.


#8

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] == "!"
True
>>>

#9

got it. thanks for your help


#10

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