![]() |
How to calculate a months' 1st, 4th, 7th day and also 1st again? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to calculate a months' 1st, 4th, 7th day and also 1st again? (/thread-30892.html) |
How to calculate a months' 1st, 4th, 7th day and also 1st again? - cananb - Nov-11-2020 Hi everyone, I just started how to learn to code. I am trying to write a program that should count the days an athlete decides to run once every three days for the whole year (On the 1st, 4th, 7th ... day of each month). At the beginning of a new month, he always runs on the 1st day of the month, even if three days have not passed since his last run. for x in range(1,32,3): print(x)This is the code I wrote for counting every three days but I couldn't find a solution for how to add the first days of months. A little help with this would be perfect. ![]() RE: How to calculate a months' 1st, 4th, 7th day and also 1st again? - bowlofred - Nov-11-2020 Your code looks fine for generating a list of days starting from 1, counting by 3. What else are you trying to do? I don't understand what you're asking by "how to add the first days of months". RE: How to calculate a months' 1st, 4th, 7th day and also 1st again? - chrischarley - Nov-12-2020 (Nov-11-2020, 08:32 PM)cananb Wrote: Hi everyone, I just started how to learn to code. Here is an approach using the calendar and datetime modules. Both these modules come with the Python installation. monthrange returns the day_of_week (here ignored by assigning to '_') and the last day number of the month. A date is created with date(yr, month, day) and the strftime method produces day_of_week, Month abbr., day_number and the year. from calendar import monthrange from datetime import date yr = 2020 for month in range(1,13): _, last_day = monthrange(yr,month) for day in range(1, last_day+1, 3): print(date(yr, month, day).strftime('%a %b %d %Y'))This outputs:
RE: How to calculate a months' 1st, 4th, 7th day and also 1st again? - perfringo - Nov-12-2020 In the face of ambiguity, refuse the temptation to guess. I just observe that there is ambiguity in terms and conditions: "an athlete decides to run once every three days for the whole year", "At the beginning of a new month, he always runs on the 1st day of the month, even if three days have not passed since his last run.". If latter is true then first condition is not valid. |