Computer Science 111, Spring Semester, 2012
Part I.
This is a difficult program that will take some time. So there is no textbook homework. However, you should spend some time reading Chapters 6,7 and 8 (pages 233-244): defining functions, decision structures, and indefinite loops, respectively. Submit this homework by 11:59pm Monday April 2 with the command submit homework6 hw6.pyPart II. Do this part with a partner if possible.
Please read this whole assignment before doing any part of it.Part 1. Change your program so that it asks the user for the
month and year only. Leave the order of these two user inputs
the same as in lab6.py (month is input first, and then year) and
also keep the error correction loops the same.
Your program will *not* ask the user for the number of days in
the month or the day of the week it starts.
Your program itself will figure out the number of days
in the month as well as the day of the week on which the month starts! Your
program is required to have functions to figure out each of these things.
In
the main function, after the month and year are obtained from the user,
two
functions are called as follows:
days_in_month = get_days (month, year)
day_of_week = get_start_day (month, year)
These function calls and assignment statements will replace the statements
in the main function from lab6.py that
prompts, reads, and error-checks for these values.
Now, you must define the two functions.
Define function get_days so that it has the month and year as formal parameters and returns the number of days in the month, an integer. The function will figure out the number of days in the specified month (for the specified year). To help you figure out the number of days in the month, there is an old "rhyme" that goes: Thirty days hath September, April, June, and November - All the rest have thirty-one, except February, which has twenty-eight, and, in leap year, twenty-nine.
How do you know whether a year is a leap year? The formula for a leap year is: evenly divisible by 4 and (either not evenly divisible by 100 or evenly divisible by 400).
Define function get_start_day so that it has the month and year as argument parameters and returns an integer. The function will figure out and return the day of the week on which the specified month (in the specified year) begins. How can you do this? Well, first note that January 1998 began on a Thursday (day 5). So you can figure out January 1 for any subsequent year by looping through the years beginning with 1998 and ending one year prior to the target year -- adding (to initial day 5) 366 for leap years and 365 for non-leap years -- and then subtract 1, get the remainder after dividing by 7, and add 1. For example, January 1 of 2003 is calculated as follows:
Year: | Leap Year? | Add: |
1998 |
No |
365 |
1999 |
No |
365 |
2000 |
Yes |
366 |
2001 |
No |
365 |
2002 |
No |
365 |
The total is 5 + 365 + 365 + 366 + 365 + 365 = 1831, subtracting 1 gives 1830, the remainder after dividing this by 7 is 3, and adding 1 gives 4 - so January 1, 2003 falls on a Wednesday.
Next, you need to find the first day for the month in question. Do this by looping through the months from January to the month prior to the target month. As you loop, add (to the number for the first day of January) the number of days for each month in between. You must use your function get_days here, to tell you how many days to add for each month. Finally, subtract 1, find the remainder after dividing by 7, and add 1 to determine the day on which the month in question begins. For example, April 1 of 2003 is calculated as follows:
Month: |
Leap Year? | Add: |
January |
|
31 |
February |
No |
28 |
March |
|
31 |
We figured out above that Jan. 1 2003 is on a Wednesday, so start with 4 and add to it 31 + 28 + 31 to get 94, subtract 1 to get 93, the remainder after dividing this by 7 is 2 and adding 1 gives 3, so April 1, 2003 falls on a Tuesday.
Part 2. Writing to a file. Change your program so that it writes the calendar into a file called "calendar.dat", in addition to printing it onto the screen. To write output into a file, you must do 3 things:
mycalendar = open("calendar.dat","w")
print(" S M T W T F S", file=mycalendar)Another example
print(" "+str(i)+"\n",file=mycalendar)
Part 3.To receive an A, make sure everything above works perfectly, then modify your program so that it prints to the file something that looks like a real calendar page, with boxes for each day. Use -'s and |'s to construct the boxes. Be sure that day numbers print inside of the boxes! And that there is blank space inside the box for someone to write a small memo.
Your final program should be in a file called hw6.py Submit this homework by the due date with the command submit homework6 hw6.py