我目前不确定用于以下问题的逻辑以及编程的新手。(目前正在学习 python)
尝试遍历给定月份的每个日期 - 比如 05/01 - 05/31 并以下面的格式打印出来。
周一到周五的日期要单独打印。星期六和星期日的日期要单独打印。
如果这个月从星期五开始 - 05/01/2020,输出应该是 那个星期的最后一个工作日。
对于 2020 年 4 月,产量将如下所示,因为 4 月的第一周从周三开始。
我设法想出以下尝试,但不确定如何进一步进行。
import sys
from datetime import date, datetime, timedelta
year = int(sys.argv[1])
month = int(sys.argv[2])
st_dt = int(sys.argv[3])
en_dt = int(sys.argv[4])
first_date = datetime(year, month, st_dt).date()
get_first_day = datetime(year, month, st_dt).isoweekday()
def daterange(startDate, endDate, delta=timedelta(days=1)):
currentDate = startDate
while currentDate <= endDate:
yield currentDate
currentDate += delta
for date in daterange(date(year, month, st_dt), date(year, month, en_dt), delta=timedelta(days=1)):
print(date)
date.py 2020 5 1 31 # script
提出了一个独立的“if 循环”,正如我之前所说,不确定如何构建更大的图景:(
if get_first_day == 1:
#print("Monday")
sec_d = first_date + timedelta(days=4)
elif get_first_day == 2:
sec_d = first_date + timedelta(days=3)
elif get_first_day == 3:
sec_d = first_date + timedelta(days=2)
elif get_first_day == 4:
sec_d = first_date + timedelta(days=2)
elif get_first_day == 5:
sec_d = first_date
#print("Friday")
else:
pass
print(f"Second date:{sec_d} ") -- which gave -- > Second date:2020-05-01
慕妹3146593
相关分类