7/15 - (REVERSE)I don't understand why my code isn't working


#1

the goal is to reverse the string:


def reverse(text):
  new = ''
  for letter in text:
    which_letter = len(text) - 1
    new = new + text[which_letter]
    which_letter = which_letter - 1 
    
  return new
  
print reverse('Hello')

when i try to print reverse('Hello") it just prints ‘o’ five times, as if the line below doesn’t work:
which_letter = which_letter -1

shouldn’t the line result in a different letter being printed every time?

thank you


#2

this line:

which_letter = len(text) - 1

is placed inside the loop in your program. As result, which_letter will always be set to the last letter each iteration of the loop, causing which_letter to be the last letter each iteration of the loop

the idea is good to decrease which_letter, but this doesn’t really work when which_letter is redefined to the last letter each iteration of the loop


#3

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