-3
while True:
    data = input()
    if data == 'Done' or data == 'done' or data == 'd':
        break
    rev = ''
    for ch in data:
        rev = ch + rev
    print(rev)
Laurel
  • 5,965
  • 14
  • 31
  • 57
Amy
  • 1

1 Answers1

2

Based on the code, it is correct the rev variable acts as reverse of the input.

given this example:

data is 'hello'

We start the rev variable as an empty string '' and we iterate the data string character by character (ch) and inside the loop we made this operation we prepend the character to the current rev variable:

So, with the hello example:

1st iteration: rev = ch(h) + rev('') -> rev now is 'h'

2nd iteration: rev = ch(e) + rev('h') -> rev now is 'eh'

3rd iteration: rev = ch(l) + rev('eh') -> rev now is 'leh'

4th iteration: rev = ch(l) + rev('leh') -> rev now is 'lleh'

5th iteration: rev = ch(o) + rev('lle') -> rev now is 'olleh'

soloidx
  • 729
  • 6
  • 12