Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python unexpected EOF while parsing

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 >>>  
like image 699
Web_Designer Avatar asked Feb 22 '11 05:02

Web_Designer


People also ask

How do I fix parse error in Python?

To fix this, simply add the missing code, whether it is a single line or multiple lines. This code will fix the error, and the program will run properly. The error is removed by adding a single statement of the print() function. We can also use the pass keyword if we don't wish to execute anything.

What is parse error in Python?

ParserError[source] Exception that is raised by an error encountered in parsing file contents. This is a generic error raised for errors encountered when functions like read_csv or read_html are parsing contents of a file.


2 Answers

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.

like image 164
simon Avatar answered Oct 20 '22 01:10

simon


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":      .... 
like image 26
Senthil Kumaran Avatar answered Oct 20 '22 01:10

Senthil Kumaran