Python日历

Python日历,python,calendar,Python,Calendar,创建一个日历程序,允许用户在三个单独的变量中输入日、月和年,如下所示 Please enter a date Day: Month: Year: 然后,要求用户使用以下格式从选项菜单中进行选择: 菜单: 该程序必须包括以下功能: 闰年:将年份作为参数,如果一年是闰年,则返回1 指向外部站点的链接。 如果不是,则为0。此信息将仅用于其他功能。 天数:此子程序将按以下顺序接受两个参数:月和年。它将返回给定月份的天数 指向外部站点的链接。 . daysunderscoreleft:这将按以

创建一个日历程序,允许用户在三个单独的变量中输入日、月和年,如下所示

Please enter a date

Day:

Month: 

Year: 
然后,要求用户使用以下格式从选项菜单中进行选择: 菜单:

该程序必须包括以下功能: 闰年:将年份作为参数,如果一年是闰年,则返回1 指向外部站点的链接。 如果不是,则为0。此信息将仅用于其他功能。 天数:此子程序将按以下顺序接受两个参数:月和年。它将返回给定月份的天数 指向外部站点的链接。 . daysunderscoreleft:这将按以下顺序接受三个参数:日、月和年。它应该计算一年中剩余的天数,并返回剩余天数的值。这不应包括用户在计数中输入的日期

def is_leap_year(year):
    return (year % 4 == 0) and (year % 100 != 0) or (year % 400 == 0)

def days_in_month(month, year):

    if month in ['September', 'April', 'June', 'November']:
        print("30")


    elif month in ['January', 'March', 'May', 'July', 'August','October','December']:
        print("31")

    elif month == 'February' and is_leap_year(year) == True:
        print("29")

    elif month == 'February' and is_leap_year(year) == False:
        print("28")

    else:
        return None

print("Please enter a date: ")
x = int(input("Day: "))
y = str(input("Month: "))

z = str(input("Year: "))

print("Menu:")
o = int(input("1)    Calculate the number of day in the given month. \n2)    Calculate the number of days left in the given year. "))


if(o == "2"):
    print (days_in_month(y,z))

这就是我目前所拥有的。我有点困惑如何找到一个月和一年中剩下的天数,我只是需要帮助。对不起,格式太乱了,我不知道如何正确格式化

您可以尝试以下方法吗:

def is_leap_year(year):
    return (year % 4 == 0) and (year % 100 != 0) or (year % 400 == 0)

def days_in_month(day, month, year):
    if month in ['September', 'April', 'June', 'November']:
        return 30 - day
    elif month in ['January', 'March', 'May', 'July', 'August','October','December']:
        return 31 - day
    elif month == 'February' and is_leap_year(year) == True:
        return 29 - day
    elif month == 'February' and is_leap_year(year) == False:
        return 28 - day
    else:
        return None

print("Please enter a date: ")
x = int(input("Day: "))
y = str(input("Month: "))

z = int(input("Year: "))

print("Menu:")
o = int(input("1)    Calculate the number of day in the given month. \n2)    Calculate the number of days left in the given year.\n"))

if(o == 1):
    print (days_in_month(x, y,z))
它会给你一个月剩下的天数

输出:

Please enter a date: 
Day: 15
Month: February
Year: 2020
Menu:
1)    Calculate the number of day in the given month. 
2)    Calculate the number of days left in the given year.
1
14

使用任何python库查找一年中剩余的天数可以吗,然后我就可以这样做了。

没关系,我想我对如何处理一年中剩余的天数有一个想法。谢谢我不知道为什么我的问题被否决了。谢谢你的帮助!但是对于一年中剩下的天数,你不能做同样的事情来找到一个月中剩下的天数吗?我想这是不能用的,你需要在几个月内迭代才能得到结果
Please enter a date: 
Day: 15
Month: February
Year: 2020
Menu:
1)    Calculate the number of day in the given month. 
2)    Calculate the number of days left in the given year.
1
14