2017-04-17 46 views
0

喜即時得到這個錯誤: 任何幫助apreciated THX對象沒有屬性chercher_canal想我的代碼時

File "C:\Users\miky.DESKTOP-70ENDO8\Google Drive\TP2 Programmation orientée objet\TP2_V3\distributeur.py", line 90, in creer_forfait_tv canal = p_poste.chercher_canal() AttributeError: 'list' object has no attribute 'chercher_canal'

代碼本身:

from canal import Canal 
from forfait_tv import ForfaitTV 
from abonne import Abonne 

class Distributeur: 
#----------- Constructeur ----------------------------- 
def __init__(self): 
    self.__canaux = None 
    self.__forfaits = None 
    #code 
    self.__canaux = [] #list 
    self.__forfaits = [] #list 

#----------- Accesseurs/Mutateurs ---------------------- 
def ajouter_canal(self,un_canal:Canal): 
    self.__canaux.append(un_canal) 


def chercher_canal(self,p_poste:int): 
    postex = None 
    poste_trouve=None 
    lstPoste = [] 
    for i in lstPoste: 
     postex=lstPoste[i] 
     if postex.get_poste()== p_poste: 
      poste_trouve=postex 
      return print(poste_trouve) 

def telecharger_canaux(self,nom_fichier:str): 

    fichierCanaux = open(nom_fichier, "r") 
    for line in fichierCanaux: 
     eleCanal = line.strip(" : ") 
     canal = Canal(eleCanal[0],str(eleCanal[1]),str(eleCanal[2]),eleCanal[3]) 
     self.__canaux.append(canal) 
     return canal 


def sauvegarder_canaux(self, nom_fichier:str): 
    canalx = None 
    numeroPost = None 
    fichCanaux = open(nom_fichier,"w") 
    for i in self.__canaux: 
     numeroPost = i.get_poste() 
     nomPoste = i.get_nom() 
     descPost = i.get_description() 
     couexPost = i.get_cout_extra() 
     fichCanaux.write(str(numeroPost)+ ":" + str(nomPoste) + ":" + str(descPost) + ":" + str(couexPost) + "\n") 

    fichCanaux.close() 



#----------- Opérations -------------------------------- 

def creer_forfait_tv(self, p_nom:str, p_coutBase:float, p_poste:list): 
    forfait = ForfaitTV(p_nom,p_coutBase) 
    self.__forfaits.append(forfait) 
    canal = None 

    for i in p_poste: 
     canal = p_poste.chercher_canal() 
     forfait.ajouter_canal(canal) 

回答

0

那是因爲你調用不是方法chercher_canal以下代碼,但列表屬性:

def creer_forfait_tv(self, p_nom:str, p_coutBase:float, p_poste:list): 
    forfait = ForfaitTV(p_nom,p_coutBase) 
    self.__forfaits.append(forfait) 
    canal = None 

    for i in p_poste: 
     canal = p_poste.chercher_canal() 
     forfait.ajouter_canal(canal) 

將該行更改爲: canal = chercher_canal(p_poste)

而且,我不是100%肯定,但也許你想使用i,而不是p_poste

+0

謝謝,我會嘗試 –

相關問題