使用循环实现:从控制台输入一个日期日期格式:2023年2月27日计算该日期为本年的第几天使用python语言
date_str = input("请输入日期,格式为:xxxx年x月x日:")
year, month, day = date_str.split("年")[0], date_str.split("年")[1].split("月")[0], date_str.split("月")[1].split("日")[0]
year = int(year)
month = int(month)
day = int(day)
days = 0
for i in range(1, month):
if i in [1, 3, 5, 7, 8, 10, 12]:
days += 31
elif i in [4, 6, 9, 11]:
days += 30
else:
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
days += 29
else:
days += 28
days += day
print("该日期为本年的第{}天".format(days))
输出:
请输入日期,格式为:xxxx年x月x日:2023年2月27日
该日期为本年的第58天
``
原文地址: https://www.cveoy.top/t/topic/hmBf 著作权归作者所有。请勿转载和采集!