这周的assignment有一个是打印日历,但是不能使用内置的calender函数,只能分段写
先写第一排partial week,再写三排full week,如果还有剩下的再写一排partial week.
""" Print the calendar for a month. Authors: #FIXME Credits: #FIXME Limitations: Treats February as always having 28 days. """ import argparse import datetime # To determine what day of week a month # begins on. # Note: For this project, module calendar # is not permitted. It basically has a function # to do the whole assignment in one line. MONTHLEN = [ 0, # No month zero 31, # 1. January 28, # 2. February (ignoring leap years) 31, # 3. March 30, # 4. April 31, # 5. May 30, # 6. June 31, # 7. July 31, # 8. August 30, # 9. September 31, #10. October 30, #11. November 31, #12. December ] parser = argparse.ArgumentParser(description="Print calendar") parser.add_argument("month", type=int, help="Month number (1-12)") parser.add_argument("year", type=int, help="Year (1800-2525)") args = parser.parse_args() # gets arguments from command line month = args.month year = args.year # What day of the week does year,month begin on? a_date = datetime.date(year, month, 1) starts_weekday = a_date.weekday() ## a_date.weekday() gives 0=Monday, 1=Tuesday, etc. ## Roll to start week on Sunday starts_weekday = (1 + starts_weekday) % 7 month_day = 1 ## Next day to print last_day = MONTHLEN[month] ## Last day to print print(" Su Mo Tu We Th Fr Sa") ### ### The first (perhaps partial) week ### for i in range(7): if i < starts_weekday : print(" ", end="") else: # Logic for printing one day, moving to next print(format(month_day, "3d"), end="") month_day += 1 print() # Newline ### ### Then the full weeks ... ### ### Pseudocode: ### While there is at least a full week left in the month ### Print each day of the week ### (incrementing month_day as above) ### for i in range(7): print(format(month_day, "3d"), end="") month_day += 1 print() for i in range(7): print(format(month_day, "3d"), end="") month_day += 1 print() for i in range(7): print(format(month_day, "3d"), end="") month_day += 1 print() ### ### Then any remaining days (partial week) ### ### Pseudocode: ### If there are any days left ### print them for i in range(7): if i < last_day: print(format(month_day, "3d"), end="") month_day += 1 else: print(" ",end="")
这是我写出来的代码,但是最后一排并不会停下来,比如我call 2013年10月的日历,打印出来的是:
Su Mo Tu We Th Fr Sa 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
这是为什么呢,是最后一排的if函数有问题么?
付费偷看金额在0.1-10元之间
一周热门 更多>