The code is working totally fine in my PyCharm 4.5.3. Could someone please tell me how could I change the line 6 so it would be compatible with Python 2? Thank you :slight_smile:

File "python", line 6
print (text[count-1:count], end="")
SyntaxError: invalid syntax

def reverse(text):
    count = 0
    for n in text:
        count = count+1
    while count != 0:
        print (text[count-1:count], end="")
        count = count -1



Python 2.7.x interactive console in lab

 > def reverse(text): 
..     count = 0 
..     for n in text: 
..         count = count+1 
..     while count != 0: 
..         print (text[count-1:count], end="") 
..         count = count -1 
  File "<stdin>", line 6
    print (text[count-1:count], end="")
SyntaxError: invalid syntax
Unknown error.

Python 3.5.x interative console on Windows 7

>>> def reverse(text):
    count = 0
    for n in text:
        count = count+1
    while count != 0:
        print (text[count-1:count], end="")
        count = count -1

>>> reverse("abcd")


well, this might get really tricky read here

the behaviour of print is drastically changed, in python3 it is a function call, where as in python2 it is a statement.

But there is another problem, to pass the exercise you need to return the reversed string... maybe you need to append to a list/string?


Hi mtf,

Thank you for the reply. I have tried your code for Python 2.7.x but is still not working on codeacademy. I guess I will need to think about the whole exercise in different way though I really appreciate your help!

Thank you.


Hi stetim94,

I actually found the suggestion of using just "," at the end of print in python2. However, this gave me a space in between the letters.

To be honest, I don't see a problem in returning the string. If I would merely change:

print (text[count-1:count], end="")`


return (text[count-1:count], end="")

That is at least in Python3.

Anyhow, thank you for the suggestion with appending lists...I think I know where this is going :slight_smile: will give it a try!


yea, i saw the suggestion for , as well, but indeed gives a problem with the spaces between the letters

The big problem is just the change they made to python3, i am afraid you will need a different solution for python2, let me know if you need more help


If I get stuck I will certainly reach out to you. Thank you very much for all your help! :slight_smile:


Actually, if I may please ask you? Is there possibility to view the desired solution somewhere on codeacademy? I came up with a solution but I am just wondering whether there isn't more preferable way :slight_smile:


there is no desired solution, you can try to optimize the code


That is how you develop and hone your skills. Expect positive results along the way (code that works) and also look back on how you got those results. The mechanics and logic used might be resource heavy, or take a lot of clock ticks or be hard to read. The ways we can improve on our code are many. The only real way to learn is to go through these challenges and never turn your back on a program. It's your program. You are the best person to improve it.


Mtf, that is really nicely said :slight_smile: however, as almost total beginner I think I would have to disagree with you. From my experience so far, I came up with solutions which while working were mostly quite cumbersome. Quite often I would do things and it was only later when I was told that it is not even recommended to approach the solution in such (my) way.

Just to illustrate, what I mean. I was trying some exercise on website called 'codewars'. The instruction of the exercise was:

For every positive integer N, there exists a unique sequence starting with 1 and ending with N and such that every number in the sequence is either the double of the preceeding number or the double plus 1.

For example, given N = 13, the sequence is [1, 3, 6, 13]..because:

3 = 2*1 +1
6 = 2*3
13 = 2*6 +1

As you can see from the screenshot (further below), I managed to come up with the solution. However, in totally reversed manner.

I think that would you said probably makes sense but for someone with minimum knowledge of the language (as myself) probably not. I would never be able to come up with the suggested/cleaner approach to the problem simply because I didn't even know that such syntax is possible.


I have a general rule that I stick to pretty steadfastly. Never use code one does not fully understand. Study and learn from it, but take the fledgling steps of learning the working parts. Once you understand it you will be able to write it. Until then, work with what you have and take the time to study it and point out its flaws or ways it can be improved. In this way you build from the ground up.

Don't try to memorize code, and definitely don't toss yours aside just because you found a more intriguing way. Many coders on codewars are extremely well trained and have been at their craft for several years. They look at problems from their perspective. We must look at problems from ours.

You should see some of the fledgling solutions I've come up with. That's why refactoring was invented. We are allowed to start with something rudimentary. It passes all the tests. Success, already. Looking at other solutions is not an embarrassment to me. It's a vista on what is out there. No point adopting an approach we haven't engrained in our own manner of thinking.

The real responsibility still falls to us to question our approach and learn how to improve it, bit by bit. Some of that learning comes from reading code written in different ways. Read up on the concepts and apply that learning back on our own work. Gradually we begin writing at an elevated level. This will never preclude good old practice. Dig up old code and refactor. Look for new ways to do the same thing the old code does.


Thank you very much for your reply. I really appreciate you took the time to write all this. I will keep it in mind - thank you for the encouraging words.

Thank you!