85

Here's my python code. Could someone show me what's wrong with it.

while 1:
    date=input("Example: March 21 | What is the date? ")
    if date=="June 21":
        sd="23.5° North Latitude"
    if date=="March 21" | date=="September 21":
        sd="0° Latitude"
    if date=="December 21":
        sd="23.5° South Latitude"
    if sd:
        print sd

And Here's what happens:

>>> 
Example: March 21 | What is the date? 
Traceback (most recent call last):
  File "C:\Users\Daniel\Desktop\Solar Declination Calculater.py", line 2, in <module>
    date=input("Example: March 21 | What is the date? ")
  File "<string>", line 0
    
   ^
SyntaxError: unexpected EOF while parsing
>>> 
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Web_Designer
  • 72,308
  • 93
  • 206
  • 262

11 Answers11

127

Use raw_input instead of input :)

If you use input, then the data you type is is interpreted as a Python Expression which means that you end up with gawd knows what type of object in your target variable, and a heck of a wide range of exceptions that can be generated. So you should NOT use input unless you're putting something in for temporary testing, to be used only by someone who knows a bit about Python expressions.

raw_input always returns a string because, heck, that's what you always type in ... but then you can easily convert it to the specific type you want, and catch the specific exceptions that may occur. Hopefully with that explanation, it's a no-brainer to know which you should use.

Reference

Note: this is only for Python 2. For Python 3, raw_input() has become plain input() and the Python 2 input() has been removed.

Tom Burrows
  • 2,225
  • 2
  • 29
  • 46
simon
  • 15,344
  • 5
  • 45
  • 67
  • It worked for me! But, I'm not sure as to why my Linux Mint 18.3 installation runs Python 2, even though I have installed Python 3... – na-no. Jun 12 '18 at 16:11
  • 2
    In Mint 18.3 (and Ubuntu Xenial that it's based on) `/usr/bin/python` is a python 2 executable, and `/usr/bin/python3` is your python 3 executable. With Mint 19, the system default python is python 3, I believe. – simon Jul 29 '18 at 00:22
  • Notice that OP's error indicates that the text provided to `input` was **empty**. I don't recall if 2.x stripped the trailing newline before evaluating the input; if not, then it would mean that the standard input had to have been closed somehow before the code ran. Otherwise, it simply suggests pressing Enter in response to the prompt and not actually providing any input. However, it is definite that the code was correctly indented originally; otherwise the stack trace would not mention `File ""` which proves that `input` was actually called. – Karl Knechtel Sep 03 '22 at 08:01
15

Indent it! first. That would take care of your SyntaxError.

Apart from that there are couple of other problems in your program.

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

  • You are using certain 8bit characters in your script like . You might need to define the encoding at the top of your script using # -*- coding:latin-1 -*- line commonly called as coding-cookie.

  • Also, while doing str comparison, normalize the strings and compare. (people using lower() it) This helps in giving little flexibility with user input.

  • I also think that reading Python tutorial might helpful to you. :)

Sample Code

#-*- coding: latin1 -*-

while 1:
    date=raw_input("Example: March 21 | What is the date? ")
    if date.lower() == "march 21":

    ....
Senthil Kumaran
  • 54,681
  • 14
  • 94
  • 131
  • Okay, that was for the Syntax Error. Let me edit for the other mistakes now. – Senthil Kumaran Feb 22 '11 at 05:59
  • 1
    The body of the loop is indented: indentation is Python’s way of grouping statements. At the interactive prompt, you have to type a tab or space(s) for each indented line. – Jeremy A. West Jul 18 '18 at 01:31
  • The code was in fact indented. People (including OP) repeatedly rolled back the code to show it improperly indented, but the exception that OP reported indicates that the code was correctly indented. The `SyntaxError` was coming from the `input` *because in 2.x, that evaluates the input as code*, and the input *was not valid code*. Since the error was coming from there, it could **not** have been coming from OP's actual code, implying that the indentation *was correct*. OP simply insisted on showing it incorrectly. – Karl Knechtel Sep 03 '22 at 07:55
8

I had this error, because of a missing closing parenthesis on a line.

I started off having an issue with a line saying: invalid syntax (<string>, line ...)? at the end of my script.

I deleted that line, then got the EOF message.

JGFMK
  • 8,425
  • 4
  • 58
  • 92
  • 1
    The question is not "what are the possible causes of this error message?" The question is "why does this code cause this error message?". – Karl Knechtel Sep 03 '22 at 07:33
  • @Karl Knechtel -as you can see 6 people have still found it useful. Googling on message... leads here. – JGFMK Sep 03 '22 at 10:16
5

I'm using the follow code to get Python 2 and 3 compatibility

if sys.version_info < (3, 0):
    input = raw_input
Guhh
  • 410
  • 7
  • 15
4

While @simon's answer is most helpful in Python 2, raw_input is not present in Python 3. I'd suggest doing the following to make sure your code works equally well in Python 2 and Python 3:

First, pip install future:

$ pip install future

Second: import input from future.builtins

# my_file.py    
from future.builtins import input
str_value = input('Type something in: ')

And for the specific example listed above:

# example.py
from future.builtins import input
my_date = input("Example: March 21 | What is the date? ")
PaulMest
  • 12,925
  • 7
  • 53
  • 50
2

I'm trying to answer in general, not related to this question, this error generally occurs when you break a syntax in half and forget the other half. Like in my case it was:

try :
 ....

since python was searching for a

except Exception as e:
 ....

but it encountered an EOF (End Of File), hence the error. See if you can find any incomplete syntax in your code.

Priyank Pathak
  • 464
  • 4
  • 17
1

i came across the same thing and i figured out what is the issue. When we use the method input, the response we should type should be in double quotes. Like in your line date=input("Example: March 21 | What is the date? ")

You should type when prompted on console "12/12/2015" - note the " thing before and after. This way it will take that as a string and process it as expected. I am not sure if this is limitation of this input method - but it works this way.

Hope it helps

1

After the first if statement instead of typing "if" type "elif" and then it should work.

Ex.

`    while 1:
    date=input("Example: March 21 | What is the date? ")
if date=="June 21":
    sd="23.5° North Latitude
elif date=="March 21" | date=="September 21":
    sd="0° Latitude"
elif date=="December 21":
    sd="23.5° South Latitude"
elif sd:
    print sd `
0

What you can try is writing your code as normal for python using the normal input command. However the trick is to add at the beginning of you program the command input=raw_input.

Now all you have to do is disable (or enable) depending on if you're running in Python/IDLE or Terminal. You do this by simply adding '#' when needed.

Switched off for use in Python/IDLE

    #input=raw_input 

And of course switched on for use in terminal.

    input=raw_input 

I'm not sure if it will always work, but its a possible solution for simple programs or scripts.

joesh
  • 171
  • 3
  • 9
0

Check the version of your Compiler.

  1. if you are dealing with Python2 then use -

n= raw_input("Enter your Input: ")

  1. if you are dealing with python3 use -

n= input("Enter your Input: ")

-2

Check if all the parameters of functions are defined before they are called. I faced this problem while practicing Kaggle.