In my opinion, you are not permitted to simply change the code that has been given in the context of python 2 being around and most popular.
There is a huge danger of decoupling questions and answers and coments from each other by doing so.
I think there are several options:
Provide a new answer, title it with "Solution for python 3".
Comment below the answer or question, saying that this code will only be working in python 2, because of reason X.
Edit the question or answer with "Note that this code runs in python 2".
Edit the question or answer by providing comments in the code like
a = raw_input("ask: ") # use input("ask: ") for python3
If the question and its answer are really specific to python 2, don't change anything, just make sure it has the python-2 tag.
When answering python questions, make them as compatible as possible with both versions. E.g. range(N)
or print(head)
will work in both versions (so the example in the question may acutally one of the rare cases where code may be changed - however best in consent with the posts author.); where necessary simply import from __future__
. When using python 3, leave a comment in the line which might be different for python 2.
Remember: Python 2 is not dead (yet), it is still used more often than you might think, especially in productive, educational and scientific environments from where the highest demand for quick solutions on SO arises.