我正在編寫一個程序來為我喜歡玩的紙牌游戲評分,并Game()使用以下方法進(jìn)行以下課程:def score(self): scores = [] for name in self.players: score = name.score_round() scores.append(score) scores = pd.concat(scores) scores = scores.groupby('Player').Score.sum() return scoresself.players__init__是在游戲中所有玩家的方法中制作的列表,每個玩家都有自己的Player()類別。當(dāng)我調(diào)用這個方法時,我收到以下錯誤:---------------------------------------------------------------------------TypeError Traceback (most recent call last) in ----> 1 game.score()~/dev/code_education/backalley/scoring.py in score(self) 91 def score(self): 92 scores = []---> 93 for name in self.players: 94 score = name.score_round() 95 scores.append(score)TypeError: 'method' object is not iterable我以為我會迭代一個列表,但我顯然錯過了一些東西。self.players 等類變量也被視為方法嗎?
3 回答

一只名叫tom的貓
TA貢獻(xiàn)1906條經(jīng)驗 獲得超3個贊
self.players
返回方法對象,但不調(diào)用它。您應(yīng)該遵循它以()
使其成為方法調(diào)用:
for name in self.players(): # Here -------------^

ibeautiful
TA貢獻(xiàn)1993條經(jīng)驗 獲得超6個贊
self.players 等類變量也被視為方法嗎?
不,但是您正在使用方法名稱score()
,然后嘗試將其附加到列表中。這就是你的問題所在。
添加回答
舉報
0/150
提交
取消