user13847144
user13847144

Reputation:

Python - Prints next 5 days excluding Mondays and Sundays

I have written a code that starts from the current day, prints on the screen and inserts in an array the following 5 days excluding Monday and Sunday. Except that in the array and in the print it always comes out the same day. Here is the code.

import datetime
import calendar

def findDay(date): 
    day = datetime.datetime.strptime(date, '%d %m %Y').weekday() 
    return (calendar.day_name[day]) 

Today = datetime.date.today()
StartDay = Today.strftime("%d %m %Y")

Days = ['Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
KeyboardDays = ['', '', '', '', '']
DayCount = 0
count = 0

while count <= 4:
    NextDay = Today + datetime.timedelta(days=DayCount)
    Day = str(findDay(StartDay))
    if Day in Days:
        KeyboardDays[count] = Day
        print(KeyboardDays[count])
        count += 1
    DayCount += 1

Upvotes: 1

Views: 203

Answers (1)

Yash
Yash

Reputation: 1281

It is simply because Day = str(findDay(StartDay)) never changes. And you never use NextDay. so your dayCount increment is of no use. So you rename NextDay to StartDay or use NextDay instead of StartDay Try this:

import datetime
import calendar

def findDay(date): 
    born = datetime.datetime.strptime(date, '%d %m %Y').weekday() 
    return (calendar.day_name[born]) 

Today = datetime.date.today()
StartDay = Today.strftime("%d %m %Y")

Days = ['Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
KeyboardDays = ['', '', '', '', '']
DayCount = 0
count = 0

while count <= 4:
    #NextDay = (Today + datetime.timedelta(days=DayCount)).strftime("%d %m %Y")
    StartDay = (Today + datetime.timedelta(days=DayCount)).strftime("%d %m %Y")
    Day = str(findDay(StartDay)) # or Day = str(findDay(NextDay))
    
    if Day in Days:
        KeyboardDays[count] = Day
        print(KeyboardDays[count])
        count += 1
    DayCount += 1

Upvotes: 3

Related Questions