【问题标题】:Count issue in my Calendar is making the days display wrong in Python我的日历中的计数问题导致日期在 Python 中显示错误
【发布时间】:2022-11-28 01:54:50
【问题描述】:

我正在做这个项目,它几乎完成了,但是从 5 月开始,日期排错了星期几。我不确定如何修复计数。按照我的设置方式,2 月计数为 7,这会导致 3 月出现问题,并导致 3 月打印完全错误。我添加了一个简单的修复程序,但这会导致 5 月在错误的日期开始,其余月份也会如此。我相信这是一个快速解决方案,只是想让另一双眼睛关注我的工作。

month_header = 'Su\tM\tTu\tW\tTh\tF\tSa'
months = { 'January':31, 'February':28, 'March':31, 'April':30, 'May':31, 
    'June':30, 'July':31, 'August':31, 'September':30, 'October':31,
    'November':30, 'December':31 }

# k is the key (month name), v is the value (month length in days)
month_daycount = 0
for k,v in months.items():
    print(k) # print the month name
    print(month_header)
    while month_daycount > 7:
        month_daycount -= 7
    feb_bug = 0
    if month_daycount == 7:
        month_daycount = 3
        feb_bug = 1
        #Fixes the Feb issue^
    count = month_daycount
    for i in range(1, v+1):    
        tabs = ''
        while month_daycount > 0:
            tabs += '\t'
            month_daycount -= 1
            
        print(tabs, i, end='\t')
        #this end will finish the parameter with whatever is in quotations
        
        count += 1
        if count == 7:
            print("\n")
            count = 0
    
    month_daycount = v
    if feb_bug == 1:
        month_daycount += 3
        feb_bug = 0
        # TODO: when you begin a new month, indent it the right number of spaces
    print('\n')

【问题讨论】:

    标签: python dictionary count


    【解决方案1】:

    具体我不确定你的代码,但我觉得你的代码可以通过使用 Python 的 datetime 库来大大简化。然后您可以简单地使用 delta 函数迭代这些天,因为您已经定义了格式,您的问题就解决了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多