python输入一个年份,python输入一个年份区间判定该区间闰年的个数

发布时间:2022-11-17

本文目录一览:

1、python输入一个年份,若是闰年,则输出该年二月的月历,若非闰年,则输出该年所有? 2、用Python,从键盘任意输入一个年,计算这个年是多少天。比如:输入2019年,要首先判断是否闰年 3、用python从键盘随机输入一个年份,输出该年有多少天(考虑闰年)? 4、编写一个Python程序,用户从键盘输入一个年份,程序输出此年份的中国生肖。

python输入一个年份,若是闰年,则输出该年二月的月历,若非闰年,则输出该年所有?

#include<stdio.h
void Judge(int y)
{
    while(1)
    {
        printf("请输入要计算的年份:\n");
        scanf("%d",y);
        if((y%100==0)&&(y%400==0)||(y%100!=0)&&(y%4==0))
            printf("%d年是闰年,该年2月份有29天\n",y);
        else
            printf("%d年是平年,该年2月份有28天\n",y);
        printf("\n");
    }
}
void main()
{
    int year;
    Judge(year);
}

用Python,从键盘任意输入一个年,计算这个年是多少天。比如:输入2019年,要首先判断是否闰年

def leap_year_or_not(year):
    # 世纪闰年:能被400整除的为世纪闰年。
    # 普通闰年:能被4整除但不能被100整除的年份为普通闰年。
    # 闰年共有366天,其他年只有365天。
    if int(year) % 400 == 0:
        return True
    elif int(year) % 100 !=0 and int(year) % 4 == 0:
        return True
    else:
        return False
def calculate_days_of_year(year):
    leap = leap_year_or_not(year)
    if leap:
        days = 366
        run = "是"
    else:
        days = 365
        run = "不是"
    print("{}年{}闰年,有{}天。".format(year, run, days))
if __name__ == "__main__":
    print("输入年份:")
    n = input()
    calculate_days_of_year(n)

运行上述代码,输入2019回车,得到以下结果:

用python从键盘随机输入一个年份,输出该年有多少天(考虑闰年)?

使用python标准库判断闰年

import calendar
year = 2019
if calendar.isleap(year):
    day_num=365
else:
    day_num=366

calendar库中的闰年算法

def isleap(year):
    """Return True for leap years, False for non-leap years."""
    return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)

编写一个Python程序,用户从键盘输入一个年份,程序输出此年份的中国生肖。

year_input=int(input("请输入年份:"))
if year_input >= 1000:
    SymbolicAnimals = year_input%12
    if SymbolicAnimals == 0:
        print("{}年是:申(猴)".format(year_input))
    elif SymbolicAnimals == 1:
        print("{}年是:酉(鸡)".format(year_input))
    elif SymbolicAnimals == 2:
        print("{}年是:戌(狗)".format(year_input))
    elif SymbolicAnimals == 3:
        print("{}年是:亥(猪)".format(year_input))
    elif SymbolicAnimals == 4:
        print("{}年是:子(鼠)".format(year_input))
    elif SymbolicAnimals == 5:
        print("{}年是:丑(牛)".format(year_input))
    elif SymbolicAnimals == 6:
        print("{}年是:寅(虎)".format(year_input))
    elif SymbolicAnimals == 7:
        print("{}年是:卯(兔)".format(year_input))
    elif SymbolicAnimals == 8:
        print("{}年是:辰(龙)".format(year_input))
    elif SymbolicAnimals == 9:
        print("{}年是:巳(蛇)".format(year_input))
    elif SymbolicAnimals == 10:
        print("{}年是:午(马)".format(year_input))
    elif SymbolicAnimals == 11:
        print("{}年是:未(羊)".format(year_input))
else:
    print("请输入大于4位数的年份")