python错误“列表索引必须是整数而不是Nonetype" [英] python error “list indices must be integers not Nonetype”

查看:38
本文介绍了python错误“列表索引必须是整数而不是Nonetype"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 新手,正在从 Udacity 学习这门语言.我想编写一个 python 程序,它接受 2 个日期并输出这 2 个日期之间的天差,假设第二个日期是后者.

I am new to python and learning the language from Udacity. I wanted to write a python program that takes 2 dates and outputs the day difference between these 2 dates, assuming the second date is latter.

它抛出一个错误说:

File /Users/gonewiththewind/Documents/days old.py", line 20, in daysBetweenDates
    currentDaysOfMonths = daysOfMonths[isLeap(year)][month - 1]
TypeError: list indices must be integers, not NoneType"
when I tried to call the function by "daysBetweenDates(1995,7,28,2018,1,26)

代码如下:

daysOfMonths = [[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]]


def isLeap(year):
    if year % 400 == 0:
        return True
    else:
        if year % 100 == 0:
            return False
        else:
            if year % 4 == 0:
                return True

def daysBetweenDates(year1, month1, day1, year2, month2, day2):
    counter = 0
    month = month1
    year = year1
    day = day1
    while(year != year2 or month != month2 or day != day2):
        currentDaysOfMonths = daysOfMonths[isLeap(year)][month - 1]

        if(day < currentDaysOfMonths):
            day = day + 1
            counter = counter + 1
            print 'counter = '+ counter
        else:
            day = 1
            if(month1 < 12):
                month = month + 1
            else:
                month = 1
                year = year + 1
            counter = counter + 1
            print 'counter = '+ counter

    return counter

推荐答案

您的 is_leap 函数中缺少 return:

You are missing a return in your is_leap function:

def isLeap(year):
    if year % 400 == 0:
        return True
    else:
        if year % 100 == 0:
            return False
        else:
            if year % 4 == 0:
                return True
            else:
                return False  # <-- here!

否则,这个函数会在那个地方隐式返回None,这是不真实的,但不是bool,因此也不是int(boolint 的子类,它首先使 0-1-index 魔术成为可能)可以用作 list 索引.顺便说一句,如果 if 块中有 return,则不需要 else:

Otherwise, this function will implicitly return None in that place, which is non-truthy, but not a bool, and therefore not an int (bool is a subclass of int, which makes the 0-1-index magic possible in the first place) that can be used as a list index. Btw, you do not need the else if there is a return in the if block:

def isLeap(year):
    if not year % 400:
        return True
    if not year % 100:
        return False
    # return not year % 4  # is also possible here
    if not year % 4:
        return True
    return False  # <-- needed to avoid None being returned

这是否更具可读性通常取决于具体情况.但在这里,有多个嵌套分支,我认为这有助于保持低缩进级别并了解正在发生的事情.

Whether this is more readable very often depends on the concrete circumstances. But here, with multiple nested branches, I think it helps keeping the indentation levels low and understanding what is happening.

这篇关于python错误“列表索引必须是整数而不是Nonetype"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆