Python Forum
How to calculate a months' 1st, 4th, 7th day and also 1st again?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to calculate a months' 1st, 4th, 7th day and also 1st again?
#1
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. Angel
Reply
#2
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".
Reply
#3
(Nov-11-2020, 08:32 PM)cananb Wrote: 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. Angel

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:

Output:
Wed Jan 01 2020 Sat Jan 04 2020 Tue Jan 07 2020 Fri Jan 10 2020 Mon Jan 13 2020 Thu Jan 16 2020 Sun Jan 19 2020 Wed Jan 22 2020 Sat Jan 25 2020 Tue Jan 28 2020 Fri Jan 31 2020 Sat Feb 01 2020 Tue Feb 04 2020 Fri Feb 07 2020 Mon Feb 10 2020 Thu Feb 13 2020 Sun Feb 16 2020 Wed Feb 19 2020 Sat Feb 22 2020 Tue Feb 25 2020 Fri Feb 28 2020 ... Sun Nov 01 2020 Wed Nov 04 2020 Sat Nov 07 2020 Tue Nov 10 2020 Fri Nov 13 2020 Mon Nov 16 2020 Thu Nov 19 2020 Sun Nov 22 2020 Wed Nov 25 2020 Sat Nov 28 2020 Tue Dec 01 2020 Fri Dec 04 2020 Mon Dec 07 2020 Thu Dec 10 2020 Sun Dec 13 2020 Wed Dec 16 2020 Sat Dec 19 2020 Tue Dec 22 2020 Fri Dec 25 2020 Mon Dec 28 2020 Thu Dec 31 2020
Reply
#4
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Illegal instruction? working code for months? korenron 4 13,033 Aug-05-2021, 09:57 AM
Last Post: korenron
  How to convert dates in odd format to months lokhtar 2 2,209 Apr-17-2021, 11:54 AM
Last Post: lokhtar
  How to groupby Months showing average order value - Pandas & matplotlib Rwood90 0 1,850 Oct-20-2020, 12:53 PM
Last Post: Rwood90
  'Age' categorical (years -months -days ) to numeric Smiling29 4 2,906 Oct-17-2019, 05:26 PM
Last Post: Smiling29

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020