Reverse function


This is my code:

def reverse(text):
  dictname = {}
  position= len(text) - 1
  for character in text:
    dictname[position]= character
    position -= 1
  for number in range(0,len(text)):
    print dictname[number],

The error message was:
Your function fails on reverse(“Python!”). It returns “None” when it should return “!nohtyP”.

When I clicked Run, the black screen showed: c b a ! n o h t y P

Where did that cba come from and why is my code not working? Could someone please help me out


Okay I made my code much simpler but it still doesn’t work:

def reverse(text):
  position= len(text)-1
  while position >= 0:
   print text[position],
   position -= 1


Your function should return a reversed string, not print it.


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