7. reverse


#1

I have tried a number of things but I can't get the thingy to iterate backwards. The program has run out of oops to show me. Some times I get its just "!" when it should be.. 'int- instead of... or like in the case below, "Your function fails on reverse("Python!"). It returns "None" when it should return "!nohtyP"."
I'm trying to understand.
I think the code below would mean:

def reverse(text):
---for i in text: #for each index in the string Python!
-------while len(text)>i: #while 7-the length of the string- is larger than the index number
-----------position = len(text) - i # position is equal to the length of text minus the index number
----------- return text[position] #return the letter on index number position

It doesn't iterate through the string, much less backwards.
The program throws an oops, in this case saying that returns NONE.
I have tried a dozen ways and the closest I get is getting a '!' but not the rest of letters in reverse order.
Could anyone explain, please?
There is, obviously something I fail to understand!!!

Many, many thanks,
DD


#2

how is the indention of the code, i get the idea it is this:

def reverse(text):
    for i in text:                             #for each index in the string Python!
        while len(text)>i:               #while 7-the length of the string- is larger than the index number
            position = len(text) - i # position is equal to the length of text minus the index number
            return text[position]   #return the letter on index number position

i am right? If i am, i would change the indention. Now your return statement is already reached after the loop is executed once. return should be on the same indent level as the for loop


#3

Thank you,
I'll be right back after trying!


#4

the loop ends when return is reached, if the indention is wrong, this means the return is reached while the loops have many more runs to make


#5

With the return statement aligned with either while loop or for loop throws:

Oops, try again. Did you create a function called reverse? Your code threw a "local variable 'position' referenced before assignment" error.

Your interpretation of my indentation was right, but however I indent the return statement I get an Oops!


#6

this code will never work, here is why:

for i in text:
    print(i)

you will see, this will print the letters. so your while loop:

while len(text)>i:

i is a letter, not a number. Which is something you can't do


#7

Thank you!

Rather confusing! isn't len(string x) the number of letters (indexes) in that string? In other words, isn't len(Python!) 7?

I went to look for other solutions around here and tried to understand their solution so I can't go back (I don't know how to, but) would it work if it was converted to a number like int(len(text))

Many thanks nevertheless


#8

len("Python!") would be 7 (you forgot the quotation marks ")

You can go back to earlier exercises if you like?

i don't understand, what do you mean by this: int(len(text))? I need to see full code, i would make a variable where i could push the hits to


#9

Thank you steim94,

Still, in the code above (the one I posted at the begining that you printed with indentations),
in the line while len(text )>i,
Isn't text there a string? Does it need to be put in between quotation marks?
And, if it is a string, len(text) when is passed Python is 7. Either I am wrong somewhere or I still don't understand.

With int(len(text)) I was asking if in the code above, text needed to be changed to an integer ( a number)

How can you go back to past exercises? once I finished one I can't go back, the program sends me to the welcome back page.

Many thanks again, I really appreciate your help.
DD


#10

No, text is a variable, which you parse into the function:

def reverse(text):

then when you can the function:

reverse("hello")

you need to indicate you have a string, so this might be easier:

text = "hello"

in both cases, text holds the string hello, so no, no quotation marks. text is a variable, which holds a string.

len(text) should already be an intereger. Make sure you call your function, otherwise len(text) = None

You can go here, once you selected the track, in the bottom left there is a button you can use. In this navigation menu you can select the exercise in the track, and there is back button which will take you to the track overview


#11

Thank you very much stetim94.!
I'm going to try going back a little and make sense of it.
If you can help me coding/understanding all this you can help anyone!
DD


#12

Take smell steps, insert a print statement here and there to see the result real quick.