Python 日历

Python Calendar

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

Please enter a date

Day:

Month: 

Year: 

然后,要求用户从使用此格式的选项菜单中 select: 菜单:

1)   Calculate the number of days in the given month.

2)   Calculate the number of days left in the given year.

程序必须包含以下功能: leap_year:以年份为参数,如果年份是闰年,则returns 1 (链接到外部站点。) 如果不是,则为 0。此信息将仅由其他功能使用。 number_of_days:该子程序将按以下顺序接受两个参数:月和年。它将 return 给定月份有多少天 (链接到外部站点。) . daysunderscoreleft:这将按以下顺序接受三个参数:日、月和年。它应该计算一年中剩余的天数,以及 return 剩余天数的值。这不应包括用户在计数中输入的日期。

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))

它会给你days left in month

输出:

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 库找到一年中剩余的天数,然后我可以做到..