MagicforestA 2022-06-28 13:10 采纳率: 100%
浏览 72
已结题

calendarclass的问题

"""
需要计算出某个月内星期几有几天,输入(年份,weekday)
weekday是指0-6,0代表星期一,6代表星期日,
求结果,运行后总抱怨
TypeError: monthdays2calendar() missing 1 required positional argument: 'month'
问题出在哪里?
"""
import calendar
class MyCalendar(calendar.Calendar):
    def __init__(self,month):
        self.__month=month
        
        self.__count=0
    def count_weekday_in_year(self,year,weekday):
        self.__year=year
        self.__weekday=weekday
        
        
        for data in MyCalendar.monthdays2calendar(self.__year,self.__month):
            if data[1]==self.__weekday:
                self.__count+=1
        return self.__count

object1=MyCalendar(3)
print(object1.count_weekday_in_year(2019,0))
  • 写回答

2条回答 默认 最新

  • CSDN专家-showbo 2022-06-28 14:26
    关注

    用下面的

    
    import calendar
    class MyCalendar(calendar.Calendar):
        def __init__(self,month):
            self.__month=month
            self.__count=0
            super().__init__()##要初始化基类
        def count_weekday_in_year(self,year,weekday):
            self.__year=year
            self.__weekday=weekday
            
            #通过类名称调用需要传入self对象,或者改为self.monthdays2calendar(self.__year,self.__month):,通过实例调用
            for weekdays in MyCalendar.monthdays2calendar(self,self.__year,self.__month):
                for date in weekdays:#monthdays2calendar返回的是七个元组的列表,其中每个元组都由日期和星期几组成。所以还得遍历一次
                    if date[0]!=0 and date[1]==self.__weekday:#注意不是本月的日期为0
                        self.__count+=1
            return self.__count
     
    object1=MyCalendar(6)
    print('2022-6星期一天数:')
    print(object1.count_weekday_in_year(2022,0))
    
    
    object1=MyCalendar(5)
    print('2022-5星期一天数:')
    print(object1.count_weekday_in_year(2022,0))
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 7月6日
  • 已采纳回答 6月28日
  • 创建了问题 6月28日

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端