python计算传入的日期是这一年的第几天

2021-02-24  本文已影响0人  以我丶之姓
import sys
def is_leap_year(year):
    return year % 4 == 0 and year %100 != 0 or year % 400 ==0
# print(is_leap_year(2000))
def which_day(year, month, date):
    days_of_month = [
        [31,28,31,30,31,30,31,31,30,31,30,31],
        [31,29,31,30,31,30,31,31,30,31,30,31]
        ][is_leap_year(year)]
    # print(days_of_month)
    total = 0
    for index in range(month - 1):
        total += days_of_month[index]
    
    return total + date
def main():
    s = input('请输入年月日,以逗号隔开:')
    # print(type(s))
    # print(s.split(','))
    f = which_day(int(s.split(',')[0]),int(s.split(',')[1]),int(s.split(',')[2]))
    print(f)

if __name__ == "__main__":
    main()
上一篇下一篇

猜你喜欢

热点阅读