2017-02-10 195 views
0

好了,所以我在學習python3和想,也許嘗試了我的第一個腳本與leapyear計算器,但我似乎無法弄清楚如何打印,如果當年是leapyear還是不算少得多接下來的兩個。Python3閏年計算器

這裏是我的代碼迄今:

#!/usr/bin/python3 

#calculate if this year is a leap year and the next 2 leap years 

from datetime import datetime 

this_year=(datetime.now().strftime('%Y')) 

def is_leap_year(year): 
    if ((year % 4 == 0) and (year % 100 != 0)) or (year % 400 == 0): 
     return "{0}, {1} is a leap year".format(True, year) 
    return "{0} is not a leap year".format(year) 

print(is_leap_year(this_year) 

所以任何人都可以指出我的錯誤(S)在這裏?非常感謝!

+0

我的歉意。我考慮過這一點,但不知道在哪裏發佈。我認爲會有一些精明的讀者肯定會提供更正,而不是解決實際問題。我的感謝是無止境的。 – 2017-02-10 14:39:09

+1

不需要道歉。事情是,你有更好的機會在那裏找到與編程相關的答案,而不是在這裏。 – schaiba

回答

3

你有日期處理例程和諸如此類的東西有一個整體庫,只需使用它們:

try: 
    datetime.date(2016, 2, 29) 
    print("is leap-year") 
except ValueError: 
    print("isn't leap-year") 

如果date()方法能夠處理2月29日,這是一個閏年,否則不是。

+0

謝謝!這似乎比我的第一次嘗試更簡單。 – 2017-02-10 14:39:49