gpt4 book ai didi

Python 解析时出现意外的 EOF

转载 作者:太空宇宙 更新时间:2023-11-03 21:14:32 25 4
gpt4 key购买 nike

这是我的Python代码。有人可以告诉我它出了什么问题吗?

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:)

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

注意:这仅适用于 Python 2。对于 Python 3,raw_input() 已变为普通 input(),而 Python 2 input() 已被删除。

关于Python 解析时出现意外的 EOF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54816061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com