所以我在代码开始时就创建了多个5个“ Player”对象,每个“ Player”对象都有大约24个不同的“ Skill”对象,我使用它来遍历我的代码
first_run = True
while True:
for i in range(len(player_name)):
if first_run is True:
player[i] = Player(player_name[i], player_url[i], player_thumbnail[i])
print(player[i].name, "'s Object Created")
data = requests.get(player_url[i])
statusCheck(data.status_code, player_name[i])
data_processed = re.split(', |\n', data.text)
storeData(data_processed, player[i], first_run)
logging.info("Sleeping for 10 Seconds")
time.sleep(10)
first_run = False
基本上在此while循环的第一次运行中,它将创建5个具有预定义名称的播放器对象,这些对象存储在上面的列表中。现在,当我的代码到达storeData(data_processed, player[i], first_run)
时,该函数遇到了一个问题,一个问题是说当创建第一个玩家对象时创建的属性不存在,它运行得非常完美,然后突然停止了工作,我没有即使更改任何内容,突然之间也不再起作用,这是函数“ storeData” (下面是日志错误)
def storeData(data_processed, player, first_run):
line_count = 0
for line in data_processed:
if line != '':
skill_data = line.split(',')
if line_count < 24:
player_skill = None
player_skill = getattr(player, skills[line_count])
player_skill.rank = skill_data[0]
player_skill.level = skill_data[1]
player_skill.xp = skill_data[2]
line_count = line_count + 1
if player_skill.last_level != player_skill.level and first_run is not True:
logging.info(player.name, "Gained A Level in ", skills[line_count])
pushLevelUp(discordUrl,skills[line_count-1], player, player_skill)
player_skill.last_level = skill_data[1]
elif line_count < 79:
player_activity = getattr(player, activity[line_count - 24])
player_activity.last_count = player_activity.count
player_activity.rank = skill_data[0]
player_activity.count = skill_data[1]
line_count = line_count + 1
下面是日志错误,我没有像我所说的那样进行任何更改,但不再找到player.skill.Overall(使用getattr)
Traceback (most recent call last):
File "*hidden*", line 42, in <module>
storeData(data_processed, player[i], first_run)
File "*hidden*", line 142, in storeData
player_skill = getattr(player, skills[line_count])
AttributeError: 'NoneType' object has no attribute 'Overall'
Process finished with exit code 1
这些是我存储“技能”数据的列表
skills = ['Overall', 'Attack', 'Defence', 'Strength', 'Hitpoints', 'Ranged', 'Prayer', 'Magic', 'Cooking',
'Woodcutting', 'Fletching', 'Fishing', 'Firemaking', 'Crafting', 'Smithing', 'Mining', 'Herblore',
'Agility', 'Thieving', 'Slayer', 'Farming', 'Runecrafting', 'Hunter', 'Construction']
这是一个简短的摘要
第一次迭代时的 player_skill = getattr(player, skills[line_count])
应该直接指向传递到storeData
函数中的玩家对象,在本例中,请使用“ player”作为对象名称...第一次迭代时的getattr直接指向player.Overall
,这将允许我调用player.Overall.level
,但是它表示在使用player.Overall
时找不到player_skill = getattr(player, skills[line_count])
这是我的.py文件https://pastebin.com/q2VEFGfk(此文件名为'runebotClassData'),而此https://pastebin.com/h7BEckgR是(runebotTest.py)
正如我所说,我什么都没做,api似乎完全照常返回了数据,并且发生了错误,请帮忙
答案 0 :(得分:0)
哇,我完全忽略了这一点,我被错误指出了错误,该错误指出了在哪里调用player [i],而不是在何处定义了它,并且如果我的测试模式为开(first_run = False),也没有定义它更加注意了宣言