Many questions start with the day of the week. Find it for each date.
What to build
A program that names the weekday of each date and counts the weekend days.
Requirements
- For each date print
DATE: WEEKDAYwith the weekday's English name. - Then print
weekend: K, the number of dates that fall on a Saturday or Sunday.
Input
Line 1: n. Then n dates in the form YYYY-MM-DD.
Output
n lines, then the count.
Constraints
1 ≤ n ≤ 1000; valid dates from 1900 to 2100.
Example
Input:
3
2024-01-15
2024-03-02
2024-03-03
Output:
2024-01-15: Monday
2024-03-02: Saturday
2024-03-03: Sunday
weekend: 2
Why it works
Parsing the text into a real date lets Python work out the calendar, including leap years, instead of the program counting days by hand.
Examples
Example 1 · Input
3 2024-01-15 2024-03-02 2024-03-03
Expected output
2024-01-15: Monday 2024-03-02: Saturday 2024-03-03: Sunday weekend: 2
Example 2 · Input
1 2000-01-01
Expected output
2000-01-01: Saturday weekend: 1
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
datetime.strptime(text, "%Y-%m-%d") parses the date; strftime("%A") gives the weekday's name.
Hint 2
weekday() is 5 for Saturday and 6 for Sunday.
Solution
Show one correct solution (try it yourself first)
from datetime import datetime
n = int(input())
weekend = 0
for _ in range(n):
text = input()
day = datetime.strptime(text, "%Y-%m-%d")
print(f"{text}: {day.strftime('%A')}")
if day.weekday() >= 5:
weekend += 1
print("weekend:", weekend)