Output weekday in python?

Hello everyone!

I'm currently working on a task where I receive a number k between 1 and 365 from a user input. Based on this, I then need to output the day of the week (the number) to the console, where 0 = Sunday, 1 = Monday, 2 = Tuesday, 6 = Saturday, etc.

I'm kind of confused here ๐Ÿ˜ก

Thanks for your input! ๐Ÿ˜€

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
6 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
MrAmazing2
2 years ago

The task is actually just about modulo.

This is the solution:

 (((userInput - 1) % 7) + 4) % 7

or somewhat simplified:

 (userInput + 3) % 7

-1 to change โ€œ1 to 365โ€ to โ€œ0 to 364โ€, since the range of weekdays also starts with zero.

Modulo 7 to determine the day of the week.

However, the zeroth day would be Sunday. Therefore, add 4 and modulo 7 to shift the weekday four days (Sun -> Thurs, Mon -> Fri, โ€ฆ), since it starts on Thursday.

MrAmazing2
2 years ago
Reply to  RaBo129

Freut mich, gerne ๐Ÿ˜€

Enzi1
2 years ago

You need more than just a number, you also need to know the year, but there is a general formula for this: https://de.wikipedia.org/wiki/Wochentagsberechnung#:~:text=Um%20den%20Wochentag%20mit%20Hilfe,ganzzahligen%20Division%20%C3%BCbrig%20bleibt%20(z.

MrAmazing2
2 years ago
Reply to  RaBo129

Leap year yes/no?

Ah nevermind, number from 1-365