-
Notifications
You must be signed in to change notification settings - Fork 9
/
class_count_weekday_in_year.py
42 lines (34 loc) · 1.26 KB
/
class_count_weekday_in_year.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# WEEKDAY COUNTER of a GIVEN YEAR
# This program asks the user to insert an year and a weekday.
# It will calculate how many weekdays there are in the given year
# By Alessandro Silvestri, assignement solved for Python Institute
import calendar
class MyCalendar(calendar.Calendar):
def count_weekday_in_year(self, year, weekday):
if weekday not in range(7):
return 'error: weekday has to be between 0 and 6'
day_result = 0
for y in range(1, 13):
for i in cal.monthdays2calendar(year, y):
for j in i:
if j[1] == weekday and j[0] != 0:
day_result +=1
return day_result
# User interface
days_list = calendar.weekheader(9).split()
print('\nYEAR WEEKDAY COUNTER')
year = input('Insert the year: ')
print('\nChoose the weekday:')
for i in range(7):
print(f'{i} for {days_list[i]}')
day = input('\n>: ')
cal = MyCalendar()
try:
day = int(day)
year = int(year)
if day in range(7) and year in range(1, 9999):
print(f'{cal.count_weekday_in_year(year, day)} {days_list[day]} in {year}')
else:
print('Input Error')
except:
print('Input Error')