python3实例(三)函数形式判断闰年

命题

输入年份,判断其是闰年还是平年

思路

  1. 输入数据
  • 整数
    • 1000-9999之间的整数
    • 超范围,重新输入
  • 非整数
    • 重新输入
  1. 判断是否为闰年
  • 闰年
    • 能被4整除且不能被100整除
    • 能被100整除
  • 非闰年
  1. 主函数
  • 调用输入函数
  • 显示判断结果
#输入年份(4位)
def input_num():
    while True:
        try:
            year=int(input("Input the year(1000-9999): "))
        #输入1000-9999内的整数,否则重新输入
            if year in range(1000,10000):
                return(year)
            else:
                continue
        #非整数抛异常,提示重新输入
        except ValueError:
            print("The except is not int ")
            continue

#判断是否为闰年
def leap_year(year):
    #平年能被4整除不能被100整除为闰年
    if year%4==0 and year%100!=0:
        print("%d is a lear year"%year)
    #世纪年(能整除1000)能被400整除为闰年
    elif year%100==0:
        print("%d is a lear year."%year)
    #其它为平年
    else:
        print("%d is a common year."%year)

def main():
    year=input_num()
    leap_year(year)
main()

猜你喜欢

转载自blog.csdn.net/Amy8020/article/details/88555064
今日推荐