Works, but CC fails it

I’m testing following code on python 2.7 and it works. But Code Academy fails this code.
Could you explain why?

def reverse(x):
return [ x[i * -1] for i in range(1, len(x) + int(1)) ]


Does the exercise instruct to return a string, in reverse of the input? The above returns a list.

Another concern is that it uses a concept (comprehension) that is not introduced until the Advanced Topics unit. Clearly the author believes we still have enough tools to complete this task.

Not sure what effect is expected? Is 1 not already an integer?

Say we want your code to return a string, the advanced nature of list comprehension aside. Have we by this point learned about the join() method?

For simplicity, consider that,

i * -1  ==  -i

so that,

>>> x = "Python is fun!"
>>> [x[-i] for i in range(1, len(x) + 1)]
['!', 'n', 'u', 'f', ' ', 's', 'i', ' ', 'n', 'o', 'h', 't', 'y', 'P']
>>> ''.join([x[-i] for i in range(1, len(x) + 1)])
'!nuf si nohtyP'

This should be sufficient to pass the lesson check if in fact a string return is expected.

Define a function called reverse that takes a string text and returns that string in reverse. For example: reverse("abcd") should return "dcba" .

You may not use reversed or [::-1] to help you with this.

Given that slicing and the built-in function are disallowed, the above is more than a leap forward, but is on the right track… Negative indices.

How do we get this into basic form without the comprehension?

>>> y = ''
>>> for i in range(1, len(x) + 1):
	y += x[-i]

>>> y
'!nuf si nohtyP'

Can we remove some of the ambiguity by generating a reversed range such that,

y += x[i - 1]


# range (start, stop, step)
>>> range(len(x), 0, -1)
[14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
>>> y = ''
>>> for i in range(len(x), 0, -1):
	y += x[i - 1]

>>> y
'!nuf si nohtyP'

Notice that now the index is positive, but the stride is negative?


Thank you for great explanation!


You’re welcome!

Be sure to drill into every aspect of the range() function in your review study.


I was able to generate the correct answer, but CC fails it. why?

new =
def reverse(text):
index = len(text) -1
while index >= 0:
index -= 1
return ‘’.join(new)

print reverse(‘poo’)

1 Like

Creating the list outside of the function means it can never be reset. Declare the array inside the function so it starts fresh on each new call.

I have the same problem. I was able to generate the correct answer but CC fails it.

def reverse(text):
list =
for letr in text:
n = len(list)
while n > 0:
print list[n - 1],
n -= 1

We are expected to return the reverse of the input string. What you code does is print, and returns None.

Some things to consider.

  1. text is a string.
  2. The length of a string can be determined without iterating it using len(string).
  3. There is no need to cast a string character to str().
  4. We can build a list from the front end using insert().

Start by defining the function and include the return line…

def reverse(text):
    result = []
    # loop code
    return result

Now consider this loop…

    for letter in text:

One final consideration is that the return value also needs to be a string. How would we convert a list to a string?


Thank you so much for the response!
Now I know where the problems are. I forgot that you can also add strings into a variable by simply using +=, so I don’t actually need to build a while loop outside, I can just use .join() instead. So thanks for that tip also.

1 Like