爬蟲與Python:(二)Python基礎篇——擴展:判斷閏年


現在做一個簡單的程序:輸入年份字符串,判斷是否為閏年。閏年的條件為什么?

  • 非整百年能被4整除
  • 整百年能被400整除

代碼如下:

 1 # 判斷是否為閏年
 2 year =int(input("請輸入一個年份:"))
 3 if year % 4 == 0 :
 4     if year%100 == 0 :
 5         if year % 400 == 0 :
 6             print('{}是閏年'.format(year)) # 整百年且能被400整除是閏年
 7         else:
 8             print('{}不是閏年'.format(year))
 9     else:
10         print('{}是閏年'.format(year)) # 能被4整除,且非整百年是閏年
11 else:
12     print('{}不是閏年'.format(year))

運行后,控制台會輸出:

請輸入一個年份:2000
2000是閏年


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM