python - 打印日历,但是不用内置的calender函数

 Binggo89 发布于 2022-10-27 16:33

这周的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函数有问题么?

2 个回答
  • assignment是什么

    2022-11-12 01:41 回答
  • for i in range(7):
            if i < last_day:
                    print(format(month_day, "3d"), end="")
                    month_day += 1
            else:
                    print("   ",end="")

    最后一排 i在0-7内取值,所以会永远小于你定的last_day(此值至少为28) 所以最后一个循环必定打印7次。

    打印使用如下循环即可

    print(" Su Mo Tu We Th Fr Sa")
    print("   "*starts_weekday, end="")
    
    for i in range(last_day):
        print(format(month_day, "3d"), end="")
        if (i+starts_weekday)%7==6:
            print()
        month_day += 1
    2022-11-12 01:41 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有