gpt4 book ai didi

解析时Python意外的EOF

转载 作者:IT老高 更新时间:2023-10-28 21:34:32 26 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/5074225/

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