解析时Python发生意外的EOF [英] Python unexpected EOF while parsing

查看:271
本文介绍了解析时Python发生意外的EOF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的python代码。

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

这就是发生的情况:

>>> 
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
Expression
,这意味着您
最终以gawd知道目标变量中的
对象是什么类型,以及可以生成大量异常

格式。因此,除非您要为临时测试将
放入
,否则您应该
使用输入只能由对Python表达式了解
的人使用。

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 始终返回字符串
,因为,哎呀,这就是您总是
键入的内容……但是您可以轻松地将
转换为您想要的特定类型,然后捕获特定的
可能发生的异常。希望
有这样的解释,这是
的明智之举,知道您应该使用

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.

参考

注意:这仅适用于Python2。对于Python 3, raw_input()已变得很简单 input()和Python 2 input()已被删除。

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

这篇关于解析时Python发生意外的EOF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆