Error: Oops, try again. Your function fails on reverse("Python!"). It returns "" when it should return "!nohtyP".

This code should work logically. Someone explain to me how it doesn't.

Plus, the code like this doesn't work either: for x in range(len(text), -1, -1)

I'm starting to get upset.

def reverse(text):
    new_str = ''
    for char in range(0, len(text), -1):
        new_str = new_str + text[char]
    return new_str


What is Python's range() Function?

range([start], stop[, step])

  • start: Starting number of the sequence.
  • stop: Generate numbers up to, but not including this number.
  • step: Difference between each number in the sequence.

now in your case
for char in range(0, len(text), -1):

straing from 0 but step is -1

and here

for x in range(len(text), -1, -1)

range() is 0-index based.
doing len(text)
you'll get an IndexError: string index out of range
beacuse text length is not 0 index based(meaning text length is 4 but the position of last character is 3)


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