嗨,我在尝试我的代码时遇到了这个错误:任何帮助都会被接受
文件"C:\Users\miky.DESKTOP-70ENDO8\Google \TP2 Programmationée objet\TP2_V3\distributeur.py",第90行,在creer_forfait_tv canal = p_poste.chercher_canal() AttributeError中:'list‘对象没有属性'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)发布于 2017-04-17 19:19:33
这是因为您不是在下面的代码中调用方法chercher_canal,而是调用一个list属性:
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)
另外,我不是百分之百确定,但也许您想使用i而不是p_poste
https://stackoverflow.com/questions/43457826
复制相似问题