# Reverse

#1

This is starting to become an annoying habit now.

It's starting get frustrating!

Anyway. I understood what I needed to do, but no idea how to achieve it. I had to come straight to the forums to look at the answer. I don't think any of this is syncing in and it's starting to get frustrated.

``````def reverse(text):
new_word = ""
for i in range((len(text) -1), -1, -1):
new_word += text[i]
return new_word

print reverse("Penri")``````

Now. in range() codecademy told me that range(start, stop, step) which makes perfect sense. Start at this index, Stop at this one, Step through it like that.

Now, reverse the word. For me it sounded like I had to change the order of range() to be range(Start at the end, Stop at the start, Step like that) but HOW does -1 do that? how can START, STOP and STEP be -1? That makes zero sense.

I feel that this section is not helping anyone out.

Can someone help me?

Thanks
PM

EDIT

``print range((len("Penri")),-1,-1)``

This example gives me 5,4,3,2,1,0. Again, 0 sense. Start is lengh, STOP is -1 STEP is -1?

Is this explained in another exercise on codecademy?

#2

This section (practice makes perfect) is trying to push learners from coders to the direction of programmers. Frustrating? Absolutely.

If you take your time, its also very valuable to learn.

``range(start, stop, step)``

you don't have to start at zero, look:

``````for i in range(10, 0, -1):
print i``````

this will output 10 till 0 (zero excluded, stop value is not included in range)

we do the same thing in reverse, we start at the end of our string (using `len()`). However, strings are zero indexed based, so if our string is `"hello"`, the length is 5 while `o` is at index 4, which is why we do `-1`

we also have `-1` for the stop value, because otherwise the `h` of index zero is is not included. (see my remark about range not including stop value)

#3

If you're saying range(10,0,-1)

Start at 10
Stop at index 0(not shown)

but why is it step -1?

Why can't step just be 1? you're still wanting it to step 1 at a time?

Also, why isn't it starting at number 9?

I understand that number 4 includes 5 indexes.

1 2 3 4 5 - index
0 1 2 3 4 - number

how can print range(5) show 4, but print range(10,0,-1) start at 10.

Sorry, I feel like I'm being really aggressive but I'm not, I just found that although codecademy wants to figure this out, it's a bit like showing someone to climb over a desk then saying "Right, go climb a mountain"

It goes from extreme hand holding to no help at all.

#4

step 1 means: `+1`. if you make the steps `+1`, you get the following condition:

``````if start < stop
# maybe demonstrating this with while is even better:
while start < stop``````

range keeps doing this, until the condition becomes false. While if you do steps of -1 you get:

``````if start > stop
# or with while, not sure which is better:
while start > stop``````

`range()` is a function someone wrote, which probably uses conditions like this under the hood. so just taking steps of 1 (+1) won't work

#5

1 = Step up
-1 = Step down

That's something I'll have to remember in the future.

``````def reverse(text):
new_word = "" #create a new varible with an empty string
for i in range((len(text) -1), -1, -1): # Take the length of the word, end at index-1, step backwards -1.
new_word += text[i] #What's going on here? Why is it += not just =?
return new_word

print reverse("Penri")``````

#6

`2` is two steps up (`+2`), `-2` is two steps down

i see you are still puzzled by this line:

``new_word += text[i]``

we can do `=` instead of `+=`:

``new_word = new_word + text[i]``

new_word is a string in which we are going to store our reversed string. So while looping over `text` in reverse, we add the letters to new_word

#7

hmmm....ok

I'll play about with the code. See if I can see what's happening.

#8

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