Python Forum
Calculating the number of day of the week - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Calculating the number of day of the week (/thread-25347.html)



Calculating the number of day of the week - RbaPhoenix - Mar-27-2020

1. It is possible to name the days 0 through 6 where day 0 is Sunday and day 6 is Saturday. You want go on a summer holiday and you want to make reservation for it. You decided to go at next Thursday (day number 4) for 12 nights, you would return home on a Tuesday (day 2).

Day index 0 1 2 3 4 5 6
Day name Sunday Monday Tuesday Wednesday Thursday Friday Saturday


Write a python script which asks for the starting day number, and how many nights you want to go to holiday residence, and it will tell you the number of day of the week you will return on.



RE: Calculating the number of day of the week - Larz60+ - Mar-27-2020

the following will convert a datetime date to day of week:
it uses today(), but you can replace with another date
>>> from datetime import datetime
>>> datetime.today()
datetime.datetime(2020, 3, 27, 13, 15, 56, 364721)
>>> datetime.today().weekday()
4



RE: Calculating the number of day of the week - stullis - Mar-27-2020

This is a math question. The day of the week is effectively a base 7 number because it resets to 0 every 7 days. We can use a modulus to calculate the remainder of any number divided by 7 to discern the progression to 7. For example, if we start on Sunday (0) and stay for 3 days, we can divided 3 by 7 and determine that the remainder is 3. Or we stay for 213 days; divide by 7... 30 and remainder 3.

To demonstrate with the example provided. You leave on Thursday (4) and stay for 12 days. If we add 4 and 12, we get 16. Divide by 7... 2 and remainder 2.

Since this is the homework forum, I won't do more. But you should have enough keywords here to devise a solution. Read up on the Python language documentation or your class materials.


RE: Calculating the number of day of the week - Larz60+ - Mar-27-2020

see: https://docs.python.org/3/library/datetime.html#datetime.date.weekday