I just wrote my first Python program a guessing game and it exits with an error I get this.

Zachary Ware zachary.ware+pylist at gmail.com
Wed Jun 5 11:38:32 EDT 2013


On Wed, Jun 5, 2013 at 10:14 AM, Armando Montes De Oca
<armandomontesdeocaiii at gmail.com> wrote:
> On Wednesday, June 5, 2013 10:40:52 AM UTC-4, Armando Montes De Oca wrote:
>> Traceback (most recent call last):
>>
>>   File "Guessing_Game.py", line 32, in <module>
>>
>>     input (enter)
>>
>>   File "<string>", line 0
>>
>>     ^
>>
>> SyntaxError: unexpected EOF while parsing
>>
>> ------------------
>>
>> (program exited with code: 1)
>>
>> This is the only place a string is used:
>>
>> else:
>>
>>       print "Sorry you loose the game."
>>
>>       computernum = str(computernum)
>>
>>       print " The computers number was!"+ computernum
>>
>>       input (enter)
>>
>>       sys.exit(0)
>> Yes I did declare and enter value it is:
>  enter = "Please Press Enter To Continue..."
>> Thank You,

Hi Armando,

There are a few problems with your question.  To be able to help you,
we really need to know your Python version, what OS you are using, how
you are running your script, and at what point you get the error in
question.  Also, just a snippet of code from the middle of the script
is useless to us, we need to see a complete, runnable program that
shows your problem.

In this particular case, though, I can deduce a few things and, I
believe, answer your question.

First, your Python version.  Judging by your use of 'print' as a
statement rather than a function, it looks like you're using Python 2,
probably 2.7.  This is a big factor in your problem.

Second, when you get your error.  I'm betting it's after you see
"Please Press Enter To Continue..." and have pressed 'enter'.

Now the kicker: I can reproduce your problem with the following
complete program:

# error_test.py
input('Press enter')
# end of error_test.py

The problem here is that you are using 'input()' in Python 2.  In
Python 2, 'input()' is equivalent to 'eval(raw_input())', which means
that anything you give to 'input' will be evaluated in the current
scope, and this is a HUGE security hole.  It can also cause serious
issues, such as you are having with your program: when you press enter
at the prompt, you pass '' (the empty string) to eval, which sees EOF
before it can actually evaluate anything, and so it raises a
SyntaxError.  The simple fix here is to replace every occurance of
'input(' with 'raw_input(', or to make a step towards Python 3
compatibility, add this to the top of your program:

try:
    input = raw_input
except NameError:
    pass

Hope this helps,

Zach



More information about the Python-list mailing list