2

As a newbie to Python, I'm kind of learning some of the differences between Python2 and 3. In working through the Python course, it seems that there are some things that need to be changed in the code to make it work in 3. Here's the code;

def clinic():
    print "In this space goes the greeting"
    print "Choose left or right"
    answer = raw_input("Type left or right and hit 'Enter'.")
    if answer == "LEFT" or answer == "Left" or answer == "left":
        print "Here is test answer if you chose Left."
    elif answer == "RIGHT" or answer == "Right" or answer == "right":
        print "Here is the test answer if you chose Right!"
    else:
        print "You didn't make a valid choice, please try again."
        clinic()

clinic()

To make this work in Python 3, the print syntax needs to be changed (add parens), but another issue that comes up is the error "NameError: global name 'raw_input' is not defined". I've seen this issue come up often in my learning. It doesn't seem to come up when I run it in Python2, but in 3 it seems to need it declared as a global. However, when I add "global raw_input" to the function, it doesn't seem to work (in other cases, it worked everytime I did it.) Can someone tell me what I'm doing wrong? Also, I've heard that declaring globals is a bad habit to get into when not necessary, so what's the best way to handle them?

Fenikso
  • 9,251
  • 5
  • 44
  • 72
Kimomaru
  • 993
  • 4
  • 14
  • 30
  • possible duplicate of [What's the difference between raw\_input() and input() in python3.x?](http://stackoverflow.com/questions/4915361/whats-the-difference-between-raw-input-and-input-in-python3-x) – Lennart Regebro Feb 26 '13 at 23:03

2 Answers2

7

raw_input() has been renamed in Python 3, use input() instead (and the old Python 2 input() was removed). See PEP 3111.

See What's new in Python 3.0 for an exhaustive overview. There is also the Dive into Python 3 overview.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

Amending Martijn's answer, here's a general trick you can do for these kind of small incompatibilities:

try:
    input_func = raw_input
except NameError:
    raw_input = input

Afterwards you can just use raw_input in your script with both Py2 and Py3. Similar things might be required for the unicode, and byte types.

Since you indicated you're interested in migration from >=Py2.7 to a Py3, you should know that Python 2.7 was mostly a Python 2.6 with lots of Py3 stuff backported.

So, while the print function technically still is a statement in Py2.7, and a function in Py3, the Py2.7 print does accept tuples. And that makes some of the Py3 syntax work in Py2.7. In short, you can just use parantheses:

print("Here is the test answer if you chose Right!")

To print an empty line, the best method working in both versions would be

print("")

To print without adding a newline by default I'm resorting back to write(), e.g.:

import sys
sys.stdout.write("no newline here")
sys.stdout.write(" -- line continued here, and ends now.\n")

On the other hand, for lots of Py3 stuff, you can actually enable the full Py3 syntax in Py2.7 by importing things from the future:

from __future__ import print_function

Then you don't need to switch between write() and print().

In real applications it all depends on if and how you have to interact with other people's code (packages, other developers in your team, code publishing requirements) and what's your roadmap for Python version changes.

Community
  • 1
  • 1
cfi
  • 10,915
  • 8
  • 57
  • 103