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

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

使用 find() 方法時出現 AttributeError

使用 find() 方法時出現 AttributeError

郎朗坤 2021-06-03 14:41:28
希望有人能解釋一下這個...我是 Python 新手,偶然發現了一項作業(通過在線課程學習):創建了卡片組并需要移除幾張卡片——我想我會在卡片組 [] 中找到 .find(str),返回它的索引 (i) 和 .remove(i)。這樣我就可以驗證代碼是如何工作的,因為我還在學習......當我使用 .find() 方法時,出現以下錯誤:AttributeError: 'list' object has no attribute 'find'但是索引方法沒有這樣的錯誤:這是兩種方法。`def indxcard(self,fcard):    '''    :return index of -1 if not found    '''    retval=-1    try:        retval=self.Carddeck.index(fcard)    except:        # value not found        retval = -1    # print only for debugging    print('in Indxcard', retval,fcard)    return retvaldef findcard(self, fcard):    '''    :return index of card -1 if not found    '''    retval = self.Carddeck.find(fcard)  # Causes an attribute error...    print('in find card', retval, fcard)    return retval...和調用代碼...print(gamedeck)print('-----------')for killzerocard in Cards.Suits:    # gamedeck.killcard('0'+killzerocard[1:])  # Uno deck only as 1  Zero (0) card for each of the colors    try:        gamedeck.findcard('0'+killzerocard)    except:        print('error thrown by find')    gamedeck.indxcard('0'+killzerocard)print('-----------')# gamedeck.shuffle()print(gamedeck)`結果:
查看完整描述

1 回答

?
千萬里不及你

TA貢獻1784條經驗 獲得超9個贊

list對象沒有find方法。這兩個strlist對象都有一個index方法,但只能strfind方法。對于字符串,這兩種方法基本相似,只是當字符串中不存在參數時,index將拋出異常并find返回-1


查看完整回答
反對 回復 2021-06-08
  • 1 回答
  • 0 關注
  • 814 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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