如何更改此代码以使其跳过周末但从周二开始

How to change this code so it skips weekends but starts on Tuesday

在我的 中,我在 python 中询问了如何执行以下操作(这是一个摘要):

I'm creating an automation in Python, that will automatically navigate to my school's website, and join the online class, etc using selenium. I've completely finished that part, and it all works perfectly. Now, I'm trying to figure out how to schedule these actions.

I have 10 classes in total. I have 6 classes in a day. My school week goes from Monday and Friday. In my timetable, there are a total of 6 days, meaning that every week, one day in the timetable gets rotated out, and I don't have it that week, and another rotates in.


So, to further explain what I mean by that, in the first week, I would have Days 1-5 Monday to Friday, but I would not have Day 6. I would have Day 6 the following Monday.

How do I go about scheduling these days (Day1 - Day6) so that they run on Monday to Friday with one day rotating out and then back in the next week (as I described above)?

这就是我得到的答案,这正是我所需要的,但是,它只适用于 first_day_of_school 是星期一的情况。

from datetime import datetime

day_of_year = datetime.now().timetuple().tm_yday
first_day_of_school = datetime(2020, 9, 15).timetuple().tm_yday
day_of_school = day_of_year - first_day_of_school
week_of_school = day_of_school // 7
day_of_week = day_of_school % 7 # 0 means Monday
day_of_term = week_of_school * 5 + day_of_week
class_index = day_of_term % 6
print(class_index)

我想知道的是,如果 first_day_of_school 是星期二,我该如何正确修改它以使其以相同的方式工作?

我知道 // 7* 5 部分是在星期一跳过周末的方式,但我无法计算出如何让它在另一天工作。在过去的 12 个小时里,我尝试了很多不同的组合,但我无法解决。我真的希望答案不只是盯着我看,因为这让我很沮丧。

我试过前后计算,但没有任何效果。 Python 这方面对我来说很新鲜。

我想 timedelta 就是您要找的东西

import datetime

day_of_year = datetime.datetime.now().timetuple().tm_yday

skip_days = 2
delta = datetime.timedelta(days=skip_days)
first_day_of_school = datetime.datetime(2020, 9, 15) + delta

first_day_of_school = first_day_of_school.timetuple().tm_yday
day_of_school = day_of_year - first_day_of_school
week_of_school, day_of_week = divmod(day_of_school, 7) 
day_of_term = week_of_school * 5 + day_of_week
class_index = day_of_term % 6
print(class_index)