Programming Assignment 1 (Month Calendar)
Calendar
Write a Python program that reads two command line parameters, month (between 1 and 12)
and year (between 1 and 9999),
and produces the calendar for specified month in the specified year.
The program should be written from basic principles without the help of any libraries other
than the ones given in the hints below.
The format of the calendar is given in the sample runs shown later.
- To read command line parameters, you can use the folowing code:
import sys
month = int(sys.argv[1])
year = int(sys.argv[2])
-
To determine day of week for the first of the month, you can use the following code:
from datetime import date
d = date(year,month,1)
day_of_week = (d.weekday()+1)%7
- To catch errors in input you should use the try-except statement as follows:
try:
month = int(sys.argv[1])
year = int(sys.argv[2])
...
...
except Exception as e:
print(str(e))
Here are some sample runs of the program:
Mac-mini:cal raj$ python3 Cal.py 7 2022
July 2022
Su Mo Tu We Th Fr Sa
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
Mac-mini:cal raj$ python3 Cal.py 10 1958
October 1958
Su Mo Tu We Th Fr Sa
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
Mac-mini:cal raj$ python3 Cal.py 15 2022
month must be in 1..12
Mac-mini:cal raj$ python3 Cal.py 10 20022
year 20022 is out of range
Mac-mini:cal raj$ python3 Cal.py abc 2022
invalid literal for int() with base 10: 'abc'
Mac-mini:cal raj$ python3 Cal.py 10 2022a
invalid literal for int() with base 10: '2022a'
Mac-mini:cal raj$ python3 Cal.py 10
list index out of range
Mac-mini:cal raj$ python3 Cal.py
list index out of range
GenAI Problem
Modify your program to also accept just the year as a command line parameter and
print the calendar for the entire year as shown in the following run:
mirage:~ rsunderraman$ python3 Cal.py 2022
2022
January February March
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 1 2 3 4 5 1 2 3 4 5
2 3 4 5 6 7 8 6 7 8 9 10 11 12 6 7 8 9 10 11 12
9 10 11 12 13 14 15 13 14 15 16 17 18 19 13 14 15 16 17 18 19
16 17 18 19 20 21 22 20 21 22 23 24 25 26 20 21 22 23 24 25 26
23 24 25 26 27 28 29 27 28 27 28 29 30 31
30 31
April May June
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 1 2 3 4 5 6 7 1 2 3 4
3 4 5 6 7 8 9 8 9 10 11 12 13 14 5 6 7 8 9 10 11
10 11 12 13 14 15 16 15 16 17 18 19 20 21 12 13 14 15 16 17 18
17 18 19 20 21 22 23 22 23 24 25 26 27 28 19 20 21 22 23 24 25
24 25 26 27 28 29 30 29 30 31 26 27 28 29 30
July August September
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 2 1 2 3 4 5 6 1 2 3
3 4 5 6 7 8 9 7 8 9 10 11 12 13 4 5 6 7 8 9 10
10 11 12 13 14 15 16 14 15 16 17 18 19 20 11 12 13 14 15 16 17
17 18 19 20 21 22 23 21 22 23 24 25 26 27 18 19 20 21 22 23 24
24 25 26 27 28 29 30 28 29 30 31 25 26 27 28 29 30
31
October November December
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 1 2 3 4 5 1 2 3
2 3 4 5 6 7 8 6 7 8 9 10 11 12 4 5 6 7 8 9 10
9 10 11 12 13 14 15 13 14 15 16 17 18 19 11 12 13 14 15 16 17
16 17 18 19 20 21 22 20 21 22 23 24 25 26 18 19 20 21 22 23 24
23 24 25 26 27 28 29 27 28 29 30 25 26 27 28 29 30 31