구문 분석 중 Python 예기치 않은 EOF
여기 내 파이썬 코드가 있습니다. 누군가가 나에게 무엇이 잘못되었는지 보여줄 수 있습니까?
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
그리고 다음과 같은 일이 발생합니다.
>>>
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
>>>
raw_input
대신 사용 input
:)
를 사용
input
하면 입력 한 데이터가 Python 표현식 으로 해석됩니다. 즉, gawd가 대상 변수의 객체 유형과 생성 될 수있는 광범위한 예외를 알고 있다는 것을 의미합니다. 당신이해야한다 그래서 NOT 사용input
임시 테스트를 위해 뭔가를 넣어하지 않는 한, 단지 파이썬 표현식에 대해 조금 아는 사람에 의해 사용된다.
raw_input
항상 문자열을 반환합니다. 왜냐하면 항상 입력하는 것이기 때문입니다. ...하지만 원하는 특정 유형으로 쉽게 변환하고 발생할 수있는 특정 예외를 포착 할 수 있습니다. 그 설명을 통해 어떤 것을 사용해야하는지 아는 것은 당연한 일입니다.
참고 : 이것은 Python 2에만 해당됩니다. Python 3의 경우 raw_input()
일반화되었으며 input()
Python 2 input()
가 제거되었습니다.
들여 쓰기! 먼저. 그것은 당신의 SyntaxError
.
그 외에도 프로그램에는 몇 가지 다른 문제가 있습니다.
raw_input
문자열을 입력으로 받아들이고 싶을 때 사용 합니다.input
파이썬 표현식 만 취하고 그것에 대해eval
수행합니다..NET과 같은 스크립트에서 특정 8 비트 문자를 사용하고 있습니다
0°
.# -*- coding:latin-1 -*-
일반적으로 코딩 쿠키라고 하는 줄을 사용하여 스크립트 상단에서 인코딩을 정의해야 할 수 있습니다 .또한 str 비교를 수행하는 동안 문자열을 정규화하고 비교하십시오. (lower ()를 사용하는 사람들) 이것은 사용자 입력에 약간의 유연성을 제공하는 데 도움이됩니다.
또한 Python 튜토리얼을 읽는 것이 도움이 될 것이라고 생각합니다. :)
샘플 코드
#-*- coding: latin1 -*-
while 1:
date=raw_input("Example: March 21 | What is the date? ")
if date.lower() == "march 21":
....
@simon의 답변은 Python 2에서 가장 유용하지만 raw_input
Python 3에는 존재하지 않지만 코드가 Python 2와 Python 3에서 똑같이 잘 작동하는지 확인하려면 다음을 수행하는 것이 좋습니다.
첫째, pip install future :
$ pip install future
둘째 : future.builtins에서 입력 가져 오기
# my_file.py
from future.builtins import input
str_value = input('Type something in: ')
위에 나열된 특정 예의 경우 :
# example.py
from future.builtins import input
my_date = input("Example: March 21 | What is the date? ")
줄에 닫는 괄호가 누락 되었기 때문에이 오류가 발생했습니다.
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.
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.
I'm using the follow code to get Python 2 and 3 compatibility
if sys.version_info < (3, 0):
input = raw_input
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
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 `
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.
Check if all the parameters of functions are defined before they are called. I faced this problem while practicing Kaggle.
참고URL : https://stackoverflow.com/questions/5074225/python-unexpected-eof-while-parsing
'Program Tip' 카테고리의 다른 글
Magento에서 매장 정보를 얻는 방법은 무엇입니까? (0) | 2020.10.22 |
---|---|
HH : MM : SS 문자열을 자바 스크립트에서만 초로 변환 (0) | 2020.10.22 |
Dart는 열거를 지원합니까? (0) | 2020.10.21 |
WPF MVVM 직선 XAML 창보기가 아닌 ContentControl + DataTemplate보기를 사용하는 이유는 무엇입니까? (0) | 2020.10.21 |
app.yaml을 사용하여 GAE에 환경 변수를 안전하게 저장 (0) | 2020.10.21 |