1. 程式人生 > >閏年 判斷的兩種方式

閏年 判斷的兩種方式

首先明確  什麼是閏年?

1、能被4整除,但不能被100整除;

2、能被400整除;

方案一:

while True:
    year = input("請輸入要判斷的年份(例如:2000):")
    if  year.isdigit():
        year = int(year)
        result = (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
        if result:
            s = "是"
        else:
            s = "不是"
        print("{0}年{1}閏年".format(year, s))
    else:
        print("請輸入年份!")
 

方案二:

while True:
    year = input("請輸入您要判斷的年份(例如:2000):")
    if  year.isdigit():
        year = int(year)
        result = (year/400 == int(year/400))or((year/4 == int(year/4)) and (year/100 != int(year/100)))
        if result:
            s = "是"
        else:
            s = "不是"
        print("{0}年{1}閏年!".format(year, s))
    else:
        print("請輸入年份!")
 

方案三:

while True:
    year = input("請輸入您要判斷的年份(例如:2000):")
    if  year.isdigit():
        yea = int(year)
        if (yea/400 == int(yea/400))or((yea/4 == int(yea/4)) and (yea/100 != int(yea/100))):
            print(year+"是閏年!")
        else:
            print(year+"不是閏年!")
    else:
        print("請輸入年份!")