確定閏年
您正在嘗試編寫一個程式來確定特定年份是否符合閏年條件。但是,當前程式碼在 Python IDLE 中執行時傳回“None”。
回想一下,閏年滿足以下條件:
<code class="python">def leapyr(n): if n%4==0 and n%100!=0: if n%400==0: print(n, "is a leap year.") elif n%4!=0: print(n, "is not a leap year.") print(leapyr(1900))</code>
<code class="python">def leapyr(n): if n%4==0 and n%100!=0: if n%400==0: return n, "is a leap year." elif n%4!=0: return n, "is not a leap year." result = leapyr(1900) print(result)</code>
使用calendar.isleap 的替代方法
Python 提供了一個內建函數名稱為calendar.isleap,明確確定給定年份是否為閏年:<code class="python">import calendar print(calendar.isleap(1900))</code>
以上是需要進行哪些修正才能讓 Python 函數正確地確定閏年?的詳細內容。更多資訊請關注PHP中文網其他相關文章!