Weekday Calculator¶
Using the functions you wrote in previous assignments and the leap year function provided, write a function to determine the day of the week a person was born given his or her birthday. The following steps should be used to find the day of the week corresponding to any date from 1901 through the present.
In the following explanation, the following terms will be helpful.
Assuming I type in a birthday as "6 10 1981"
:
The month is
6
.The day of the month is
10
.The year is
1981
.
Instructions¶
Tip: I would comment-out the input section and leave the “automatic tests”. This way, when you run the program you don’t need to waste time typing anything in. The tests will tell you if your function is working or not. When you are convinced that it is working, you can uncomment that input section to turn the assignment in.
First, paste in your month_name
, weekday_name
, and month_offset
functions from your previous exercises.
In the weekday
function:
Find the number of years since
1900
, and put it into a variable calledyears_since_1900
. Simply subtract1900
from the year (yyyy
) to get this.Divide the number of years since
1900
by4
. Put the quotient in a variable calledtotal
. For example, if the person was born in1983
,years_since_1900
would be83
. Divide83
by4
and store20
intotal
. The result must be an integer, so use floor division.Also add the number of years since
1900
tototal
.Add the day of the month to
total
, but because we need to0
-index the day of the month, we need to also subtract1
.Using the function
month_offset()
you wrote, find the “month offset” and add it tototal
.If the year is a leap year and if the month you are working with is either January or February, then subtract
1
from thetotal
. You can use the functionis_leap()
provided to determine if the year is a leap year.Find the remainder when
total
is divided by 7. Pass this remainder to the functionweekday_name()
you wrote to determine the day of the week the person was born. Store this in a variable calledweekday
.Finally, build up a single String value containing the whole date (day of week, month, day, year). You’ll need to use the function
month_name()
you wrote to show the month name rather than its number.Return that String value.
Whew. Here’s some code to get you started.
Welcome to Mr. Mitchell's fantastic birth-o-meter!
All you have to do is enter your birthday, and it will tell you
the day of the week on which you were born.
Some automatic tests....
12 10 2003 => Wednesday December 10, 2003
2 13 1976 => Friday February 13, 1976
2 13 1977 => Sunday February 13, 1977
7 2 1974 => Tuesday July 2, 1974
1 15 2003 => Wednesday,January 15, 2003
10 13 2000 => Friday October 13, 2000
Now it's your turn! What's your birthday?
Birthday (mm dd yyyy):
mm: 11
dd: 11
yyyy: 2010
You were born on Thursday November 11, 2010!
©2021 Daniel Gallo
This assignment is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License.
Adapted for Python from Graham Mitchell’s Programming By Doing