Reverse func


reverse func

it doesn't work
please tell me my mistake
thanks for your helps

def reverse(text):
    while i<=len(text)-1-i:


Your mistake is that you are saying while i is less than or equal to a number that is decreasing as i is increasing.

For example if your word was "hello" it would print the "o" while i is equal to 0 and then length of hello -1-0 is 4 then the "l" i would be 1 while the length of hello -1-1 would be 3 then it would reach a point where both i and the length of hello minus 3 would be the same on the second "l" meaning only half the word was reversed.

You need to find a way to reverse the message without using the variable i in the loop or else it will end it prematurely.

I hope this helps without giving it away, reply if you do not understand or need more infomation. :slight_smile:


len(text)-1-i is constant during the loop because for each loop len(text) increase +1 but it would be ok by -i


for example in second run of the loop hello is not hello anymore!! it is helloh with 5 character!


Oh I see now for one you are not printing it backwards you are printing i at the start when i is equal to 0 so its printing it forwards again you need to add the last part of text to the string first rather than the character that is in the first index as that would write "hellohello" and youd just be returning hello again.

You need it reversed so find a way to store the length of text -i so its backwards.


yeah you are right I got it
how ever this code should return hello at the end but it does not why?


it should return it backwards not forwards so it should be olleh


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