Wilson Cheung
Wilson Cheung

Reputation: 1

Python 3 Input Function and Sublime Text Build System

I have a question on Sublimetext3 build systems. I have the following code(below) that takes a response from the user. I have Python3 installed and was using that as the build system. I had initially used the function input() since Python3 replaced Python2's raw_input. This compiles on Sublime text console.

start = input("Would you like to play? (Y/N) ")
if(start.lower() == "y"):
    print(start)

Since Sublime text is unable to take a response from the user I used the REPL for Python3 as the build system. However, I couldn't get the above code to work. It consistently gave me a

NameError: name 'Y' is not defined

The only way I could get it to pass was putting a user input using quotations, ie "y".

However, when I changed input to raw_input using the REPL build system it miraculously worked(see below). It seems like a mix of Python 3 and Python 2 since the print function print() still uses parentheses.

Does anyone know how to get an input without quotation marks? Can anyone explain why the code below mixes Python3 and Python2?

start = raw_input("Would you like to play? (Y/N) ")
if(start.lower() == "y"):
    print(start)

Upvotes: 0

Views: 711

Answers (1)

Dash Winterson
Dash Winterson

Reputation: 1295

In python3 raw_input was renamed to input and python2's input was dropped. Python2's version parses the input as code which is why you're receiving this error, you need to use raw_input.

Alternatively you can hack it like this so it's cross-compatible.

try:
   input = raw_input
except NameError:
   pass

you can also read from sys.stdin if you want a more organized way of doing it.

Upvotes: 2

Related Questions