我已经使用Reticality建议的修改更新了此代码。我想访问hands_count的值,它位于数据包[]和/或下面的数组类型[]的数组中。
def history2packets(self, history, game_id, cache):
game_index = 0
player_list_index = 7
packets = []
for event in history:
self.event = event
type = event[0]
if type == "game":
(type, level, hand_serial, hands_count, time, variant, betting_structure, player_list, dealer, serial2chips) = event
if len(serial2chips) > 1:
nochips = 0
for (serial, chips) in serial2chips.iteritems():
if serial == 'values':
continue
packets.append(PacketPokerPlayerChips(game_id = game_id,
serial = serial,
bet = nochips,
money = chips))
packets.append(PacketPokerInGame(game_id = game_id,
players = player_list))
if self.previous_dealer == dealer:
previous_dealer = -1
else:
previous_dealer = self.previous_dealer
packets.append(PacketPokerDealer(game_id = game_id,
dealer = dealer,
previous_dealer = previous_dealer))
self.previous_dealer = dealer
packets.append(PacketPokerStart(game_id = game_id,
hand_serial = hand_serial,
hands_count = hands_count,
time = time,
level = level))
并在另一个函数中使用该值:
def autoDeal(self):
self.cancelDealTimeout()
if not self.allReadyToPlay():
for player in self.game.playersAll():
if player.getUserData()['ready'] == False:
for avatar in self.avatar_collection.get(player.serial):
if self.factory.verbose > 1:
self.message("Player %d marked as having a bugous PokerProcessingHand protocol" % player.serial)
avatar.bugous_processing_hand = True
if self.event[2] != 0:
self.beginTurn()
self.update()
else:
reactor.callLater(10, self.beginTurn)
self.update()
我仍然会收到错误,但却有另一个错误。
2015-02-19 22:10:11-0500 [-] Unhandled Error
Traceback (most recent call last):
File "/usr/lib/python2.6/dist-packages/twisted/application/app.py", line 445, in startReactor
self.config, oldstdout, oldstderr, self.profiler, reactor)
File "/usr/lib/python2.6/dist-packages/twisted/application/app.py", line 348, in runReactorWithLogging
reactor.run()
File "/usr/lib/python2.6/dist-packages/twisted/internet/base.py", line 1170, in run
self.mainLoop()
File "/usr/lib/python2.6/dist-packages/twisted/internet/base.py", line 1179, in mainLoop
self.runUntilCurrent()
--- <exception caught here> ---
File "/usr/lib/python2.6/dist-packages/twisted/internet/base.py", line 778, in runUntilCurrent
call.func(*call.args, **call.kw)
File "/usr/lib/python2.6/dist-packages/pokernetwork/pokertable.py", line 738, in autoDeal
if self.event[2] != 0:
exceptions.AttributeError: PokerTable instance has no attribute 'event'
两个函数都在同一个模块和类中。还有什么可能是错的?
答案 0 :(得分:0)
变量event
在history2packets
中定义,但不是全局变量。因此,当您在不同的函数中引用具有此名称的变量时,会引发NameError
。
答案 1 :(得分:0)
变量event
在方法history2packets
的本地范围内定义。如果您在另一个范围(例如函数或方法)中引用它,它将引发NameError
,因为它未在该范围中定义。
解决这个问题的一个快速方法是在两个方法的顶部放置一行global event
,但这通常不赞成。另一种方法是从event
返回history2packets
并生成autoDeal
autoDeal(self, event)
的参数。这意味着您可以致电:
e = history2packets(...)
# 5 lines later
autodeal(e)
另一种方法是假设history2packets
和autoDeal
是同一个类的2个方法。在history2packets
:
packets = []
for event in history:
self.event = event
type = event[0]
然后,制作autoDeal
def autoDeal(self):
self.cancelDealTimeout()
if not self.allReadyToPlay():
# Some lines later
if self.event[2] != 0: # This is the changed line
# Same as before