24
loading...
This website collects cookies to deliver better user experience
Caledar_builder.py
import datetime
# Set up the constants:
DAYS = ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday',
'Friday', 'Saturday')
MONTHS = ('January', 'February', 'March', 'April', 'May', 'June', 'July',
'August', 'September', 'October', 'November', 'December')
print('|| Calender-Builder ||')
while True: # Loop to get a year from the user.
print('Enter the year for the calendar:')
response = input('> ')
if response.isdecimal() and int(response) > 0:
year = int(response)
break
print('Please enter a numeric year, like 2023.')
continue
while True: # Loop to get a month from the user.
print('Enter the month for the calendar, 1-12:')
response = input('> ')
if not response.isdecimal():
print('Please enter a numeric month, like 3 for March.')
continue
month = int(response)
if 1 <= month <= 12:
break
print('Please enter a number from 1 to 12.')
def getCalendarFor(year, month):
calText = ''
calText += (' ' * 34) + MONTHS[month - 1] + ' ' + str(year) + '\n'
calText += '...Sun.....Mon....Tue..Wed...Thur....Fri....Sat..\n'
weekSeparator = ('+----------' * 7) + '+\n'
blankRow = ('| ' * 7) + '|\n'
currentDate = datetime.date(year, month, 1)
while currentDate.weekday() != 6:
currentDate -= datetime.timedelta(days=1)
while True: # Loop over each week in the month.
calText += weekSeparator
dayNumberRow = ''
for i in range(7):
dayNumberLabel = str(currentDate.day).rjust(2)
dayNumberRow += '|' + dayNumberLabel + (' ' * 8)
currentDate += datetime.timedelta(days=1) # Go to next day.
dayNumberRow += '|\n' # Add the vertical line after Saturday.
# Add the day number row and 3 blank rows to the calendar text.
calText += dayNumberRow
for i in range(3): # (!) Try changing the 4 to a 5 or 10.
calText += blankRow
# Check if we're done with the month:
if currentDate.month != month:
break
# Add the horizontal line at the very bottom of the calendar.
calText += weekSeparator
return calText
calText = getCalendarFor(year, month)
print(calText) # Display the calendar.
weekSeparator
The horizontal line string that separate weeks:blankRow
The blank rows have ten spaces in between the | day separators:while currentDate.weekday() != 6:
Get the first date in the month. (The datetime module handles all the complicated calendar stuff for us here.)
currentDate = datetime.date(year, month, 1)
Roll back currentDate until it is Sunday. (weekday() returns 6 for Sunday, not 0.)dayNumberRow
is the row with the day number labels:for i in range(3):
(!) Try changing the 4 to a 5 or 10.calText = getCalendarFor(year, month)
print(calText)
# Display the calendar.
# Save the calendar to a text file:
calendarFilename = 'calendar_{}_{}.txt'.format(year, month)
with open(calendarFilename, 'w') as fileObj:
fileObj.write(calText)
print('Saved to ' + calendarFilename)