In my opinion, you are not permitted to simply change the code that has been given in the context of pythonPython 2 being around and most popular.
There is a huge danger of decoupling questions and answers and comments from each other by doing so.
I think there are several options:
Provide a new answer, title it with "Solution for python 3".
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.
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 with "Note that this code runs in Python 2".Edit the question or answer by providing comments in the code like
Edit the question or answer by providing comments in the code likea = raw_input("ask: ") # use input("ask: ") for python3
a = raw_input("ask: ") # use input("ask: ") for python3
In any case, do not change the code.
If the question and its answer are really specific to pythonPython 2, don't change anything, just make sure it has the python-2python-2.x tag.
When answering pythonPython 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 be one of the very rare cases where code may be changed - however best in consent with the posts author.); where necessary simply import from __future__
. When using pythonPython 3, leave a comment in the line which might be different for pythonPython 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.
To simply put some numbers into play, here is a graph from a 2016 python-developers-survey: