0

I have a slight problem trying to run a python program from the terminal on my Mac. When my '.py' program has a 'input ("press the enter key to find out.")' command the terminal gives the following error message once you've pressed the 'return' key.

    Traceback (most recent call last):
      File "word_problems.py", line 6, in <module>
        input ("press the enter key to find out.")
      File "<string>", line 0

        ^
    SyntaxError: unexpected EOF while parsing

Can someone explain where the problem is?

Thanks in advance.

joesh
  • 171
  • 3
  • 9
  • possible duplicate of: [Python unexpected EOF while parsing](http://stackoverflow.com/questions/5074225/python-unexpected-eof-while-parsing) – djf Jun 23 '13 at 15:39
  • Thanks, I had looked around stackoverflow but hadn't seen that. – joesh Jun 23 '13 at 15:55

2 Answers2

1

In python 2.7, input() is identical to eval(raw_input()).

Thus, when you hit return, you actually enter '', and:

>>> eval('')
Traceback (most recent call last):
File "<PythonForiOS-Input>", line 1, in <module>
  File "<string>", line 0

    ^
SyntaxError: unexpected EOF while parsing

Instead, use raw_input().

TerryA
  • 58,805
  • 11
  • 114
  • 143
1

Use raw_input instead of input when you want accept string as an input. input takes only Python expressions and it does an eval on them.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • Do you mean that in my original .py program I have to foresee the problem and use 'raw-input' each time, instead of the normal 'input' command? Isn't there any way around that as surely that means I make my program Terminal friendly only ....? – joesh Jun 23 '13 at 15:54
  • Great, tested it and it works fine. But as I said is there no other method? Of course it now works fine in Terminal, but I get the same error code in IDLE. The big difference is that at least I understand why. Big thanks. – joesh Jun 23 '13 at 16:00
  • Ok, I don't know if this is correct but ... I notice that if I add at the beginning of the program the command 'input=raw_input' everything works fine. This means you can switch it on and off with the '#' command when running your program depending on which Terminal or IDLE. – joesh Jun 23 '13 at 16:03