python eval函数

输入格式:

输入在一行中按照格式“yyyy/mm/dd”(即“年/月/日”)给出日期。

注意:闰年的判别条件是该年年份能被4整除但不能被100整除、或者能被400整除。闰年的2月有29天。

输出格式:

在一行输出日期是该年中的第几天。

代码:

date=input()
year,month,day=date.split("/")
year=eval(year)
month=eval (month)
day=eval (day)
he=0
arr1 =[0,31,29,31,30,31,30,31,31,30,31,30,31]
arr2 =[0,31,28,31,30,31,30,31,31,30,31,30,31]
if(year%4==0 and yaer%100!=0):
    for i in range(1,month):
        he+=arr1[i]
    he+=day
    print(he)
else:
    for i in range(1,month):
        he+=arr2[i]
    he+=day
    print(he)

运行情况:

2009/03/02
Traceback (most recent call last):
  File "F:/python/练习/21008.py", line 17, in <module>
    month=eval (month)
  File "<string>", line 1
    03
     ^
SyntaxError: invalid token
>>> 若改为如下输入则不报错:
======================= RESTART: F:/python/练习/21008.py =======================
2009/3/2
61

自己思考了一下,感觉分割后month=03,day=02,再调用eval函数,会认为他们是八进制,

可以以上面的形式输入,或改为int()

原文地址:https://www.cnblogs.com/yfz1552800131/p/5360895.html