forked from bloominstituteoftechnology/Intro-Python-I
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcal.py
More file actions
40 lines (33 loc) · 1.03 KB
/
cal.py
File metadata and controls
40 lines (33 loc) · 1.03 KB
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
32
33
34
35
36
37
38
39
40
# Use the 'calendar' module to draw calendars to the console
# https://docs.python.org/3.6/library/calendar.html
#
# Use the sys module to look for command line arguments in the `argv` list
# variable.
#
# If the user specifies two command line arguments, month and year, then draw
# the calendar for that month.
# Stretch goal: if the user doesn't specify anything on the command line, show
# the calendar for the current month. See the 'datetime' module.
# Hint: this should be about 15 lines of code. No loops are required. Read the
# docs for the calendar module closely.
import sys
import calendar
# Parse command line
l = len(sys.argv)
if l == 2:
month = None
year = int(sys.argv[1])
elif l == 3:
month = int(sys.argv[1])
year = int(sys.argv[2])
else:
print("usage: cal.py [month] year")
sys.exit(1)
# Make a new calendar
c = calendar.TextCalendar()
if month != None:
# If the user specified a month, print that month
c.prmonth(year, month)
else:
# Otherwise just print it for the year
c.pryear(year)