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):

  再次运行代码,成功~

posted @ 2020-07-06 20:02  qingyun_guo  阅读(459)  评论(0编辑  收藏  举报