-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy path14_cal.py
More file actions
56 lines (47 loc) · 2.06 KB
/
14_cal.py
File metadata and controls
56 lines (47 loc) · 2.06 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
The Python standard library's 'calendar' module allows you to
render a calendar to your terminal.
https://docs.python.org/3.6/library/calendar.html
Write a program that accepts user input of the form
`14_cal.py [month] [year]`
and does the following:
- If the user doesn't specify any input, your program should
print the calendar for the current month. The 'datetime'
module may be helpful for this.
- If the user specifies one argument, assume they passed in a
month and render the calendar for that month of the current year.
- If the user specifies two arguments, assume they passed in
both the month and the year. Render the calendar for that
month and year.
- Otherwise, print a usage statement to the terminal indicating
the format that your program expects arguments to be given.
Then exit the program.
Note: the user should provide argument input (in the initial call to run the file) and not
prompted input. Also, the brackets around year are to denote that the argument is
optional, as this is a common convention in documentation.
This would mean that from the command line you would call `python3 14_cal.py 4 2015` to
print out a calendar for April in 2015, but if you omit either the year or both values,
it should use today’s date to get the month and year.
"""
import sys
import calendar
from datetime import datetime
month = datetime.today().month
year = datetime.today().year
print(sys.argv)
if len(sys.argv) == 3:
month = int(sys.argv[1])
year = int(sys.argv[2])
elif len(sys.argv) == 2:
month = int(sys.argv[1])
elif len(sys.argv) > 3:
print("Too many arguments detected. To use this script you must pass a month and/or a year in integer format: 14_cal.py [4] [2020] \
\
Printing the calendar for the current month and year.")
else:
print("No arguments detected. To use this script you must pass a month and/or a year in integer format: 14_cal.py [4] [2020] \
\
Printing the calendar for the current month and year.")
print(f'month: {month} | year: {year}')
cal = calendar.TextCalendar().formatmonth(year, month)
print(cal)