Форум сайта python.su
Есть задача
Разработать функцию create_calendar_page(month,year)
какая принимает 2 аргумента – целые числа – месяц (нумерация начинается с 1) и год, и возвращает оператором return 1 строку, которая содержит страницу календаря на этот месяц.
Если месяц и год не заданы, использовать сегодняшние значения.
“Страница”, которая возвращается, имеет следующий формат:
Это значение является одной строкой с переносами строки \n, пробили после числа 28 отсутствующие. Лишние пробили в конце пид-рядкив или всей строки, как и лишние пустые строки недопустимые.
Тесты из некорестними данными не проводятся.
————————————————————————————————————————-
Я сделал вот так:
import calendar def create_calendar_page(year, month): calendars = calendar.month(month, year) return calendars
import datetime def create_calendar_page(month=0,year=0): first_week={ 0:'01 02 03 04 05 06 07', 1:' 01 02 03 04 05 06', 2:' 01 02 03 04 05', 3:' 01 02 03 04', 4:' 01 02 03', 5:' 01 02', 6:' 01'} list_of_high=[1,3,5,7,8,10,12] list_of_low=[4,6,9,11] x=None y=None finish='' week='' first='' list_days='' temp='' z=None before_first_date='' finish_n='' list_of_second=[] list_of_second_new=[] xom=None Flag=True if month==0 and year==0: xom=datetime.datetime.today() x=xom.strftime("%Y, %m") Flag=False elif year==0: xom=datetime.datetime.today() year=xom.strftime("%Y") x=year+', '+str(month) Flag=False if Flag==False: year=x[0:(x.find(','))] month=x[(x.find(',')+2):len(x)] month=str(month) month=month.replace('0','') y=datetime.date(int(year), int(month),1).weekday() week='MO TU WE TH FR SA SU' for i in first_week: if i==y: first=first_week[i] if int(month) in list_of_high: days_in_month=32 elif int(month) in list_of_low: days_in_month=31 elif int(month)==2: if int(year)%4!=0: days_in_month=29 else: days_in_month=30 z=(8-y) for k in range(days_in_month): list_of_second.append(k) list_of_second_new=list_of_second[z:] for j in range(len(list_of_second_new)): if len(str(list_of_second_new[j]))==1: temp='0'+str(list_of_second_new[j]) else: temp=str(list_of_second_new[j]) if (j+1)%7==0: finish_n=finish_n+temp+'\n' else: if list_of_second_new[j]==list_of_second_new[len(list_of_second_new)-1]: finish_n=finish_n+temp else: finish_n=finish_n+temp+' ' finish='--------------------\n%s\n--------------------\ \n%s\n%s' %(week,first,finish_n) return finish
Отредактировано Xryst (Апрель 29, 2015 13:39:58)
Офлайн