Unexpected Python EOF on parsing

Here is my Python code. Can anyone show me what's wrong with him.

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 is 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 >>> 
+51
python eof
Feb 22 2018-11-11T00:
source share
9 answers

Use raw_input instead of input :)

If you use input , then the data that you type is interpreted as Python Expression , which means that you end up with gawd knows what type of object in your target variable and heck from a wide range of exceptions that can be thrown. So you should NOT use input unless you are using something for temporary testing, so it will be used only by those who know the bit about Python expressions.

raw_input always returns a string because hell you always type ... but then you can easily convert it to the specific type you want and catch specific exceptions that may occur. With hope with this explanation, it is not knowing what you should use.

Link

Note: this is only for Python 2. For Python 3, raw_input() became simple input() and Python 2 input() was removed.

+76
Feb 22 '11 at 5:08
source share

Get it back! . This will take care of your SyntaxError .

In addition, there are several other problems in your program.

  • Use raw_input if you want to accept a string as input. input accepts only Python expressions and eval on them.

  • In your script, you use certain 8-bit characters, like . You may need to define the encoding at the top of your script using the line # -*- coding:latin-1 -*- , usually referred to as the cookie code.

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

  • I also find that reading the Python manual might be useful to you. :)

Code example

 #-*- coding: latin1 -*- while 1: date=raw_input("Example: March 21 | What is the date? ") if date.lower() == "march 21": .... 
+11
Feb 22 2018-11-11T00:
source share

Although @simon's answer is most useful in Python 2, raw_input not in Python 3. I would 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 above:

 # example.py from future.builtins import input my_date = input("Example: March 21 | What is the date? ") 
+2
Apr 01 '16 at 0:26
source share

I try to answer in general, not related to this question, this error usually occurs when you break the syntax in half and forget the other half. As in my case it was:

 try : .... 

as python was looking for

 except Exception as e: .... 

but he ran into EOF (End Of File), so an error. See if you can find any incomplete syntax in your code.

+2
02 Sep '17 at 19:17
source share

After the first if statement, instead of entering "if", enter "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 ` 
+1
Jun 06 '16 at 0:04
source share

I use the following code to get compatibility with Python 2 and 3.

 if sys.version_info < (3, 0): input = raw_input 
+1
Sep 20 '17 at 3:29 on
source share

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

Now all you have to do is disable (or enable) depending on whether you are working in Python / IDLE or Terminal. You do this by simply adding "#" when necessary.

Disabled for use in Python / IDLE

  #input=raw_input 

And of course, it is included for use in the terminal.

  input=raw_input 

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

0
Jun 23 '13 at 16:21
source share

I ran into the same thing, and I realized what the problem was. When we use method input, the answer we must enter must be in double quotes. Like in your line date=input("Example: March 21 | What is the date? ")

You should enter, when prompted on the "12/12/2015" console - pay attention to " before and after. Thus, it will take it as a string and process it as expected. I'm not sure if this is a limitation of this input method but it works that way.

Hope this helps

0
Feb 22 '15 at 8:46
source share

Make sure that all function parameters are defined before they are called. I ran into this problem while practicing Kaggle.

-2
Sep 17 '16 at 10:47
source share



All Articles