zoukankan      html  css  js  c++  java
  • Python错误——TypeError: is_leap_year() takes 0 positional arguments but 1 was given

    问题描述:

    运行代码:

    def is_leap_year():
        """
        判断指定的年份是不是闰年
    
        :param year: 年份
        :return: 闰年返回True平年返回False
        """
        return year % 4 == 0 and year % 100 != 0 or year % 400 ==0
    
    def which_day(year, month, date):
        
        days_of_month = [
            [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
            [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        ][is_leap_year(year)]
        total = 0
        for index in range(month - 1):
            total += days_of_month[index]
        return total + date
    
    def main():
        print(which_day(1991, 12, 11))
        print(which_day(1994, 9, 6))
        
    if __name__ == '__main__':
        main()
    

      计算指定的年月日是这一年当中的第几天,

    提示如下错误:

    TypeError: is_leap_year() takes 0 positional arguments but 1 was given
    

      

    问题分析:

    报错的意思是:is_leap_year()这个函数不需要参数,但是函数却被传递了一个参数。

    掉头检查代码,会发现:

    def is_leap_year():
    

      这里出了问题,我们对其进行修改:

    def is_leap_year(year):
    

      再次运行代码,成功~

  • 相关阅读:
    遇到屏蔽selenium的站点如何突破
    subprocess.Popen stdout重定向内容实时获取
    thinkphp Composer安装指南
    职场片
    php。。。
    多线程相关
    狂刷1000题~~2
    狂刷1000题~~1
    关于eclipse中看不到源码的问题
    一篇看懂++i i++
  • 原文地址:https://www.cnblogs.com/qingyun-guo/p/13256905.html
Copyright © 2011-2022 走看看