October 29, 2021 11:53 pm GMT
Original Link: https://dev.to/sagordondev/calendar-maker-5g9
Calendar Maker
# calendar_maker.py# This program lets a user create a monthly calendar, saved to a text file# ready to print.# by: Scott Gordonimport datetimeDAYS = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday,")MONTHS = ( "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December",)print("***** Calendar Maker *****")# Loop to get the year from userwhile True: 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, i.e. 2028.") continue# Loop to get the month from userwhile True: print("Enter the month for the calendar, 1-12:") response = input("> ") if not response.isdecimal(): print("Please enter a numeric month, i.e. 1 for January.") continue month = int(response) if 1 <= month <= 12: break print("Please enter a number from 1 to 12.")def get_calendar_for(year, month): cal_text = "" # Add month and year to top of calendar. cal_text += (" " * 34) + MONTHS[month - 1] + " " + str(year) + "
" # Add days of week labels to calendar. cal_text += """+----------+----------+----------+----------+----------+----------+----------+| Sunday | Monday | Tuesday |Wednesday | Thursday | Friday | Saturday |
""" week_separator = ("+----------" * 7) + "+
" blank_row = ("| " * 7) + "|
" # Get first date in month current_date = datetime.date(year, month, 1) # Roll back current_date until it's equal to Sunday. while current_date.weekday() != 6: current_date -= datetime.timedelta(days=1) while True: # Loop over each week in month cal_text += week_separator # Create row with day number labels day_number_row = "" for i in range(7): day_number_label = str(current_date.day).rjust(2) day_number_row += "|" + day_number_label + (" " * 8) # Go to next day current_date += datetime.timedelta(days=1) day_number_row += "|
" # Add day number row and 3 black rows to calender text. cal_text += day_number_row for i in range(3): cal_text += blank_row # Check if done with month if current_date.month != month: break # Add horizontal line to bottom cal_text += week_separator return cal_textcal_text = get_calendar_for(year, month)print(cal_text)# Save to text filecalendar_file_name = f"calendar_{year}_{month}.txt"with open(calendar_file_name, "w") as file_obj: file_obj.write(cal_text)print("Saved to " + calendar_file_name)
Photo by Eric Rothermel on Unsplash
Original Link: https://dev.to/sagordondev/calendar-maker-5g9
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To