亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

迭代列表時“方法”對象不可迭代

迭代列表時“方法”對象不可迭代

嗶嗶one 2023-09-12 10:31:33
我正在編寫一個程序來為我喜歡玩的紙牌游戲評分,并Game()使用以下方法進行以下課程: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()類別。當我調用這個方法時,我收到以下錯誤:---------------------------------------------------------------------------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貢獻1906條經驗 獲得超3個贊

self.players返回方法對象,但不調用它。您應該遵循它以()使其成為方法調用:

for name in self.players():
    # Here -------------^


查看完整回答
反對 回復 2023-09-12
?
ibeautiful

TA貢獻1993條經驗 獲得超6個贊

self.players 等類變量也被視為方法嗎?

不,但是您正在使用方法名稱score(),然后嘗試將其附加到列表中。這就是你的問題所在。


查看完整回答
反對 回復 2023-09-12
?
小怪獸愛吃肉

TA貢獻1852條經驗 獲得超1個贊

該錯誤來自于說我已經進行了__repr__設置。不過還是謝謝你的幫助!



查看完整回答
反對 回復 2023-09-12
  • 3 回答
  • 0 關注
  • 114 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號