Write a program using Python that prompts the user to enter a date (day, month,
ID: 3755089 • Letter: W
Question
Write a program using Python that prompts the user to enter a date (day, month, and year). Your program will print out the day of the week for that date.
You do not need to validate the input. That means you can assume the year will be entered as a four-digit number in the range 1900 through 2100 inclusive.
the month will be one of the strings "January", "February", . . ., "December".
the day will be an integer between 1 and 31 inclusive.
the user will not enter a date that doesn't exist (such as November 31, or February 29 in a non-leap year).
Algorithm:
This algorithm was developed by Rev. Zeller.
Let "a" be an integer based on the month of the year. For the months January through December, set a = 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10 (in that order).
Let "b" be the day of the month, exactly as entered by the user.
Let "c" be the last two digits of the year; for instance, if the year is 2018, c should equal 18. Important exception: if the month is January or February, subtract one from the year. (In order to handle leap years correctly, the algorithm treats January and February as though they are the last two months of the previous year. That's why you do the assignments for "a" in that funny way.)
Let "d" be the first two digits of the year. (Make sure you calculate this AFTER you subtract one if you had to make any adjustments for January or February.)
Some sample calculations: for July 31, 1929, you should get a = 5, b = 31, c = 29, and d = 19. Similarly, for January 3, 1988, you should get a = 11, b = 3, c = 87, and d = 19.
Then calculate the following quantities:
If you did this correctly, r gives you the day of the week. r = 0 represents Sunday, r = 1 represents Monday, and so on.
You can test your program using dates from this year's calendar, or days that you're familiar with (like the day you were born, if you happen to know the day of the week). You can also use an online calendar to try dates from various years.
Expected output:
Format your output so that, if the user were to enter April 1, 1899, your output would look exactly like the following:
Explanation / Answer
Here is the Python code
--------------------------------------------------------------------------------------------------------------------------------
def find_weekday(yr,mnth,dy):
weekday=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
month_dictionary={"January":11,"February":12,"March":1,"April":2,"May":3,"June":4,"July":5,"August":6,"September":7,"October":8,"November":9,"December":10}
a=int(month_dictionary.get(mnth))
b=int(dy)
if mnth=="January" or mnth=="February":
c=(int(yr)-1)%100
d=int(str((int(yr)-1))[:2])
else:
c=int(yr)%100
d=int(str(yr)[:2])
w = (13 * a - 1 ) // 5
x = c // 4
y = d // 4
z = w + x + y + b + c - 2 * d
r = z % 7
r = (r + 7) % 7
print("The day of the week is ",weekday[r],".")
m=input("Please enter the year (an integer): ")
n=input("Please enter the month (a string): ")
o=input("Please enter the day (an integer):")
find_weekday(m,n,o)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.