I want the program to pause and wait until you press any key to continue, but raw_input() is going away, and input() is replacing it. So I have
var = input("Press enter to continue") and it waits until I press enter, but then it fails with SyntaxError: unexpected EOF while Parsing
.
This works OK on a system with Python 3, but this is linux Python 2.6 and I hate to have to code in raw_input() since it is going away.
Any suggestions?
Asked
Active
Viewed 1.5k times
6

David Andrei Ned
- 799
- 1
- 11
- 28

Dag
- 1,101
- 4
- 11
- 13
-
5Please post the **actual** code and **actual** error messages that you're **actually** getting. – S.Lott Nov 04 '10 at 03:07
-
This shouldn't be a syntax error - Python 2.6 supports the `input` function. Could you show us the rest of your code please? – Smashery Nov 04 '10 at 03:14
-
1@smashery: it will still cause a syntax error if the entered expression contains an syntax error, for example just pressing enter – recursive Nov 04 '10 at 03:15
-
Ah, seems I misread the question. Cheers. – Smashery Nov 04 '10 at 03:17
-
1Probably useful for others: http://stackoverflow.com/questions/1394956/how-to-do-hit-any-key-in-python – James Broadhead Feb 07 '12 at 23:11
3 Answers
9
Use this
try:
input= raw_input
except NameError:
pass
If raw_input
exists, it will be used for input. If it doesn't exist, input
still exists.

S.Lott
- 384,516
- 81
- 508
- 779
-
-
+1 Although I think it would have been better if there was a `__future__` import for this, there isn't, so this is the best way – John La Rooy Nov 04 '10 at 04:17
3
you could do something on the line of ...
def myinput(prompt):
try:
return raw_input(prompt)
except NameError:
return input(prompt)
... but don't.
Instead, just use raw_input()
on your program, and then use 2to3 to convert the file to python 3.x. That will convert all the raw_input()
s for you and also other stuff you might be missing.
That's the recommended way to keep a software working on both python 2 and python 3 and also keep sanity.

nosklo
- 217,122
- 57
- 293
- 297
-
but if I just move the file between two boxes, I hate to have to convert all the time – Dag Nov 04 '10 at 03:14
-
No, it does an eval of the input. You'll need to change how input is defined. http://docs.python.org/library/functions.html?highlight=input#input – dcolish Nov 04 '10 at 03:18