英文:
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]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论