Reverse function


#1

https://www.codecademy.com/courses/learn-python/lessons/practice-makes-perfect/exercises/reverse?action=lesson_resume&link_content_target=interstitial_undefined

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


#2

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

#3

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


#4

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