0
votes

Given a week number, (1st, 2nd, …), the day on which the 1st of the month falls (1 for Monday, 2 for Tuesday, …), and the number of days in the month: return a string consisting of the day of the month for each day in that week, starting with Monday and ending with Sunday. Week number represents the weeks in the months.

I have done the following functions: My code for these functions are below.

import datetime
from datetime import datetime
import calendar
from datetime import date

def week(week_num, start_day, days_in_month):
    week_string = ""

    if week_num == 1:
        if start_day == 1:
            week_string = "1 2 3 4 5 6 7"
        elif start_day == 2: 
            week_string = "  1 2 3 4 5 6"
        elif start_day == 3:
            week_string = "    1 2 3 4 5"
        elif start_day == 4:
            week_string = "      1 2 3 4"
        elif start_day == 5:
            week_string = "        1 2 3"
        elif start_day == 6:
            week_string = "          1 2"
        elif start_day == 7:
            week_string = "            1"

    elif week_num == 2:
        if start_day == 1:
            week_string = "8 9 10 11 12 13 14"
        elif start_day == 2: 
            week_string = "7 8 9 10 11 12 13"
        elif start_day == 3:
            week_string = "6 7 8 9 10 11 12"
        elif start_day == 4:
        #carry on in the above way, but this doesn't seem efficient

    return week_string

def main():
    month_name = input("Enter month:\n")
    year = eval(input("Enter year:\n"))


if __name__=='__main__':
    main()

Does anyone have any ideas on how to do the function? I need to return a string value

Another idea I had:

def week(week_num, start_day, days_in_month):
    week_string = ""
    if week_num == 1:
        week_string = ""
        day = start_day

        for i in range(1, 8 -start_day+1):
            week_string = week_string + str(i) + " "
        week_string = "{0:<20}".format(week_string)
    return week_string

An example of the input and output of this function:

week(1, 3, 30)

returns the string

' 1 2 3 4 5'
week(2, 3, 30)

returns the string

' 6 7 8 9 10 11 12’

The whole calendar should look like the following:

Mo Tu We Th Fr Sa Su
      1  2  3  4  5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30

This is for April 2020

The following are also stipulated:

week(week_num, start_day, days_in_month)

Given a week number, (1st, 2nd, …), the day on which the 1st of the month falls (1 for Monday, 2 for Tuesday, …), and the number of days in the month, return a string consisting of the day of the month for each day in that week, starting with Monday and ending with Sunday.

main()

Obtain the name of a month and a year from the user and then print the calendar for that month by obtaining the number of weeks and then obtaining the week string for each.

1

1 Answers

1
votes

You can iterate over all days in month and print it week by week:

DAYS = ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su']

def week(start_day, days_in_month):

    print(' '.join(DAYS))
    w = ['  ' for i in range(start_day-1)]
    for day in range(1, days_in_month+1):
        w.append(f'{day}' if day > 9 else f'{day} ')
        if len(w) == 7:
            print(' '.join(w))
            w = []
    print(' '.join(w))

Test:

>>>week(1, 30)
Mo Tu We Th Fr Sa Su
1  2  3  4  5  6  7 
8  9  10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30

>>>week(6, 31)
Mo Tu We Th Fr Sa Su
               1  2 
3  4  5  6  7  8  9 
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31