TypeError: ‘NoneType’ object has no attribute ‘__getitem__’

huangapple go评论61阅读模式
英文:

TypeError: ‘NoneType’ object has no attribute ‘__getitem__’

问题

I am having an issue where I am simply trying to return if two values in two lists are the same, but I have getting the TypeError issue.

def check_cond_2(day_shifts, night_shifts, day):
    return day_shifts[day] == night_shifts[day]

TypeError: 'NoneType' object has no attribute 'getitem'

I added a print statement with the condition before return to check if it's the problem with the condition but it seems to print true or false as I expected, and all I need to do is to return that Boolean but it produces the error on the line of return statement.

英文:

I am having an issue where I am simply trying to return if two values in two lists are the same, but I have getting the TypeError issue.

def check_cond_2(day_shifts, night_shifts, day):
    return day_shifts[day] == night_shifts[day]

TypeError: ‘NoneType’ object has no attribute ‘_getitem_’

I added a print statement with the condition before return to check if it’s the problem with the condition but it seems to print true or false as I expected, and all I need to do is to return that Boolean but it produces the error on the line of return statement.

答案1

得分: 1

你遇到的 `TypeError` 错误可能是由于 `day_shifts``night_shifts` 中的一个是 `None` 导致的错误信息表明 `None` 没有 `_getitem_` 属性该属性用于通过索引访问元素为了解决这个问题在尝试访问它们的元素之前你应该检查 `day_shifts``night_shifts` 中是否有一个是 `None`。

def check_cond_2(day_shifts, night_shifts, day):
    if day_shifts is None or night_shifts is None:
        return False
    else:
        return day_shifts[day] == night_shifts[day]
英文:

The TypeError you're encountering is probably due to either day_shifts or night_shifts being None. The error message suggests that None does not have the _getitem_ attribute, which is used to access elements using indexing. To resolve this, you should check if either day_shifts or night_shifts is None before attempting to access their elements.

def check_cond_2(day_shifts, night_shifts, day):
    if day_shifts is None or night_shifts is None:
        return False
    else:
        return day_shifts[day] == night_shifts[day]

huangapple
  • 本文由 发表于 2023年5月25日 15:48:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76329999.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定