Coding for KidsCoding for Kids
Creative LevelsChallengesTeacher's Guide
Vote for features
Advanced Python Development Course
Chapter
>
Level

String & Time Modules
Calendar Module

Objective

Check calendar and set dates for some of the delivery timelines by using the Calendar module.

There are several shipments that arrived and timetables that need to be met so they may be distributed around the new farm construction. Previously we used the date module to set up shipping labels, here we’re going to set timetables using the calendar module. In order to make use of the module’s classes and functions use import calendar to import all relevant materials.

For our purposes, we’ll be using the following calendar module functions:

  • calendar.calendar(): Displays the full calendar for the year, takes one (1) argument, that being the year you would like to display.
  • calendar.month(): Displays a calendar for a single month, takes two (2) arguments, the year and the month.
  • calendar.isleap(): Checks if a year is a leap year, takes one (1) argument, that being the year to check.
  • calendar.leapdays(): Checks the number of leap days accumulated across years, takes two (2) arguments being the years spanning the period you would like to check.
  • itermonthdays(): Uses Calendar() class, iterates through all the days in the month, takes two arguments, the year and month.
  • itermonthdates(): Uses Calendar() class, iterates through all the dates in the month, takes two arguments, the year and month.
  • monthdayscalendar(): Uses Calendar() class, iterates through all the weeks in the month, takes two arguments, the year and month. Stores the data in a nested list.
  • monthdays2calendar(): Same as previous function but it stores weeks and days together in a two dimensional list.

To start off, walk to the gold X mark and check the calendar, create a variable named cal and store the value of calendar.calendar() and add the year 2026 as an argument, like this: cal = calendar.calendar(2026) . Use the display() function to display the cal variable you just set up.

After checking and displaying the calendar, go to the light X mark and check the spreadsheet on the desk. Use the read() function to check the memo for the scheduled date month of the supply. Create two variables, one named year and one named month and populate them with the information you read.

Create a variable named calendar_month and store the value of the calendar.month() function using the year and month variables as arguments. Use the display() function to display the calendar month which will be used to set the distribution timetables.

Walk to the dark X mark over the green carpet, here we’ll be writing down some data points to ensure the timetables are not offset. Create two variable, one named leap_year and one named leap_days. For the leap_year variable store the result of calendar.isleap() and set 2026 as an argument. This will allow you to verify if the current project year is a leap year. For the leap_days variable, store the value of calendar.leapdays() and add in the values: 2020 and 2027 as arguments. This verifies the amount of leap days that have occurred throughout the entire project duration. Use the pre-written write() function and add the leap_year and leap_days to chard down the data.

Walk to the dark X mark over the red carpet and face the desk, here we’ll be outlining the month’s schedule. Create a variable named date and store the value of calendar.Calendar() , this creates a class object that we can reference later. Create another variable named day_roster and add a blank string quotations, this will allow us to store the roster of days in a string. Like this: day_roster = "" .

Pre-written in the editor there is a for loop and iterating though the calendar and outlining the dates. Use the itermonthdays() function and add the year and month variables you created earlier as arguments. Add missing items as needed to add up the roster tally.

for day in dates.itermonthdays(-insert value-, -insert value-): entry = "Day: %s \n" % (day) day_roster += entry

Once the loop is set up, follow this up by creating a duplicate of this loop however replace the itermonthdays() function with itermonthdates() and add the year and month variables as arguments. For the entry variable, have it record the data like this: entry = "%s \n" % (day). These will give us the full dates of the project duration. Use the pre-written display() function with the day_roster variable to display the full roster of project days in the calendar.

Walk to the the dark X mark over the blue carpet and face the memo, here we’ll be setting up the project follow up work weeks catalogued in lists. Create a variable named weeks and store the value of calendar.Calendar() , this creates a class object that we can reference with the functions. Take the month variable and replace it’s value with 7 in order to move up the timetable we’re working with, like this: month = 7 .

Create a variable named week_roster and add a blank string quotations, this will allow us to store the roster of weeks in a string. Like this: week_roster = "" . Pre-written in the editor there is a for loop and iterating though the calendar and outlining the weekdays. Use the monthdayscalendar() function and add the year and month variables you created earlier as arguments. Add missing items as needed to add up the roster tally.

for day in weeks.monthdayscalendar(-insert value-, -insert value-): entry = "%s \n" % (day) week_roster += entry

Once the loop is set up, follow this up by creating a duplicate of this loop however replace the monthdayscalendar() function with monthdays2calendar() and add the year and month variables as arguments. These will give us the full dates of the project duration. Use the pre-written display() function with the week_roster variable to display the full roster of project weekdays in the calendar in order to complete the level.

Code book