首页 > 解决方案 > 为什么我偶尔会收到 NoneType 错误?

问题描述

我无法理解我的程序的Attribute Error: None Type的起源。我正在运行一系列模拟,并且大多数情况下,当我运行 <50 个循环时,不会出现错误。但是,偶尔,或者在更大的模拟中,我会返回一个错误,它会说......

AttributeError: 'NoneType' object has no attribute 'name'
AttributeError: 'NoneType' object has no attribute 'regular_score'

这些属性已被较早地实例化,并且在大多数模拟中都可以正常工作。

该项目的目的是返回一个包含所有模拟摘要的 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

我对此的理解是似乎调用了一个没有该属性的变量,但是当循环成功运行大约 25 次模拟时,没有出现错误,并且第一列的机会/最后一列的机会 = 100,这表明没有调用隐藏变量。

这是 10 个对象之一的示例,所有 10 个对象都遵循相同的顺序。

Player_A = TL(name='Player A', scores=np.array([]))

我确实想过尝试,除了捕获错误的序列,但我不确定返回 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

一个模拟返回的梯形图样本是:

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

标签: pythonpandasobjectattributesnonetype

解决方案


我相信我已经解决了这个问题,我认为这与某些函数的疏忽有关,我不小心调用了两个 if 语句而不是 if、elif 和 else。一旦我纠正了我不再遇到这个问题。

谢谢!


推荐阅读