为什么我偶尔会返回 NoneType 错误?
Why am I sporadically being returned a NoneType Error?
我无法理解 Attribute Error: None Type 我的程序的来源。我正在 运行 进行一系列模拟,大多数时候当我 运行 <50 循环时,不会出现错误。然而,偶尔,或者在更大的模拟中,我 returned 了一个错误,它会说...
AttributeError: 'NoneType' object has no attribute 'name'
AttributeError: 'NoneType' object has no attribute 'regular_score'
这些属性已较早实例化,并且在大多数模拟中都正常工作。
该项目的目标是 return 一个包含所有模拟摘要的 DataFrame。下面是 DataFrame 的 子集 。
Projected Finish Chance of Finals Chance at a Bye Chance of First Chance of Last
Player_A 3.88 80.0 36.0 40.0 0.0
Player_B 4.56 76.0 28.0 12.0 8.0
Player_C 5.40 68.0 20.0 4.0 12.0
Player_D 5.72 60.0 12.0 4.0 8.0
Player_E 5.88 64.0 12.0 4.0 20.0
错误的回溯是:
Traceback (most recent call last):
File "/Users/tomrogers/Desktop/Python/primary/tempCodeRunnerFile.py", line 512, in <module>
finishing_postions(250)
File "/Users/tomrogers/Desktop/Python/primary/tempCodeRunnerFile.py", line 454, in finishing_postions
winner.append(grand_final())
File "/Users/tomrogers/Desktop/Python/primary/tempCodeRunnerFile.py", line 373, in grand_final
player_1 = first_preliminary()
File "/Users/tomrogers/Desktop/Python/primary/tempCodeRunnerFile.py", line 350, in first_preliminary
player_2_score = player_2.regular_score()
AttributeError: 'NoneType' object has no attribute 'regular_score'
它指向这个函数,但如您所见,我没有调用不存在的属性。
def grand_final():
player_1 = first_preliminary()
player_2 = second_preliminary()
# print(
# f"{'Simulating Grand Final'} {'Between' + ' ' + player_1.name + ' & ' + player_2.name} ")
player_1_score = player_1.regular_score()
player_2_score = player_2.regular_score()
if player_1_score > player_2_score:
return player_1.name
if player_2_score > player_1_score:
return player_2.name
我对此的理解是似乎调用了一个不具有该属性的变量,但是当循环成功 运行s 通过 25 次左右的模拟时,没有错误出现并且机会第一列/最后一列的机会均 = 100,这表明没有隐藏变量被调用。
这是 10 个对象之一的示例,所有 10 个都遵循相同的顺序。
Player_A = TL(name='Player A', scores=np.array([]))
我确实想过尝试一下,除了捕获错误的顺序,但我不确定如何使用 returning NoneType.
如果我可以提供更多详细信息,请告诉我。
非常感谢!
编辑:
def second_preliminary():
player_1 = eval(ladder['Coach'].iloc[1])
player_2 = second_elimination()
player_1_score = player_1.regular_score()
player_2_score = player_2.regular_score()
if player_1_score > player_2_score:
return player_1
if player_2_score > player_1_score:
return player_2
def second_elimination():
""" simulate the finals """
player_1 = eval(ladder['Coach'].iloc[2])
player_2 = eval(ladder['Coach'].iloc[4])
player_1_score = player_1.regular_score()
player_2_score = player_2.regular_score()
if player_1_score > player_2_score:
return player_1
if player_2_score > player_1_score:
return player_2
一个模拟的 returned 阶梯样本是:
print(ladder)
Coach F A Pct Pts
Team
Player_A Player A 7316 7023 104.17 16
Player_B Player B 7619 7436 102.46 16
Player_C Player C 7524 7272 103.47 12
Player_D Player D 7116 7102 100.20 12
Player_E Player E 7343 7593 96.71 12
Player_F Player F 7616 7276 104.67 8
Player_G Player G 7763 7532 103.07 8
Player_H Player H 7507 7342 102.25 8
Player_I Player I 7111 7681 92.58 8
Player_J Player J 7244 7902 91.67 0
我相信我已经解决了这个问题,我认为这与某些函数的疏忽有关,在这些函数中我不小心调用了两个 if 语句,而不是 if、elif 和 else。一旦我纠正我不再遇到这个问题。
谢谢!
我无法理解 Attribute Error: None Type 我的程序的来源。我正在 运行 进行一系列模拟,大多数时候当我 运行 <50 循环时,不会出现错误。然而,偶尔,或者在更大的模拟中,我 returned 了一个错误,它会说...
AttributeError: 'NoneType' object has no attribute 'name'
AttributeError: 'NoneType' object has no attribute 'regular_score'
这些属性已较早实例化,并且在大多数模拟中都正常工作。
该项目的目标是 return 一个包含所有模拟摘要的 DataFrame。下面是 DataFrame 的 子集 。
Projected Finish Chance of Finals Chance at a Bye Chance of First Chance of Last
Player_A 3.88 80.0 36.0 40.0 0.0
Player_B 4.56 76.0 28.0 12.0 8.0
Player_C 5.40 68.0 20.0 4.0 12.0
Player_D 5.72 60.0 12.0 4.0 8.0
Player_E 5.88 64.0 12.0 4.0 20.0
错误的回溯是:
Traceback (most recent call last):
File "/Users/tomrogers/Desktop/Python/primary/tempCodeRunnerFile.py", line 512, in <module>
finishing_postions(250)
File "/Users/tomrogers/Desktop/Python/primary/tempCodeRunnerFile.py", line 454, in finishing_postions
winner.append(grand_final())
File "/Users/tomrogers/Desktop/Python/primary/tempCodeRunnerFile.py", line 373, in grand_final
player_1 = first_preliminary()
File "/Users/tomrogers/Desktop/Python/primary/tempCodeRunnerFile.py", line 350, in first_preliminary
player_2_score = player_2.regular_score()
AttributeError: 'NoneType' object has no attribute 'regular_score'
它指向这个函数,但如您所见,我没有调用不存在的属性。
def grand_final():
player_1 = first_preliminary()
player_2 = second_preliminary()
# print(
# f"{'Simulating Grand Final'} {'Between' + ' ' + player_1.name + ' & ' + player_2.name} ")
player_1_score = player_1.regular_score()
player_2_score = player_2.regular_score()
if player_1_score > player_2_score:
return player_1.name
if player_2_score > player_1_score:
return player_2.name
我对此的理解是似乎调用了一个不具有该属性的变量,但是当循环成功 运行s 通过 25 次左右的模拟时,没有错误出现并且机会第一列/最后一列的机会均 = 100,这表明没有隐藏变量被调用。
这是 10 个对象之一的示例,所有 10 个都遵循相同的顺序。
Player_A = TL(name='Player A', scores=np.array([]))
我确实想过尝试一下,除了捕获错误的顺序,但我不确定如何使用 returning NoneType.
如果我可以提供更多详细信息,请告诉我。
非常感谢!
编辑:
def second_preliminary():
player_1 = eval(ladder['Coach'].iloc[1])
player_2 = second_elimination()
player_1_score = player_1.regular_score()
player_2_score = player_2.regular_score()
if player_1_score > player_2_score:
return player_1
if player_2_score > player_1_score:
return player_2
def second_elimination():
""" simulate the finals """
player_1 = eval(ladder['Coach'].iloc[2])
player_2 = eval(ladder['Coach'].iloc[4])
player_1_score = player_1.regular_score()
player_2_score = player_2.regular_score()
if player_1_score > player_2_score:
return player_1
if player_2_score > player_1_score:
return player_2
一个模拟的 returned 阶梯样本是:
print(ladder)
Coach F A Pct Pts
Team
Player_A Player A 7316 7023 104.17 16
Player_B Player B 7619 7436 102.46 16
Player_C Player C 7524 7272 103.47 12
Player_D Player D 7116 7102 100.20 12
Player_E Player E 7343 7593 96.71 12
Player_F Player F 7616 7276 104.67 8
Player_G Player G 7763 7532 103.07 8
Player_H Player H 7507 7342 102.25 8
Player_I Player I 7111 7681 92.58 8
Player_J Player J 7244 7902 91.67 0
我相信我已经解决了这个问题,我认为这与某些函数的疏忽有关,在这些函数中我不小心调用了两个 if 语句,而不是 if、elif 和 else。一旦我纠正我不再遇到这个问题。
谢谢!