[Tutor] Would you please help me understand my error

Ricardo Aráoz ricaraoz at gmail.com
Sat Apr 24 01:02:47 CEST 2010


Marco Rompré wrote:
> Its supposed to be a object magasin (shop in french) with some golf
> items in it
>
>
>  class Magasin:
>     """
>     Le concept magasin pour la gestion d'inventaire des items de golf.
>     """
>     def __init__(self, nom ="", items =[] ):
>         self.nom = nom
>         self.items = items
>
>     def set_nom(self, nom):
>         self.nom = nom
>
>     nom = property(None, set_nom)
>
>     def set_items(self, items):
>         self.items = items
>
>     items = property(None, set_items)
>
>     def __str__(self):
>         return self.nom
>
> class Item:
>     """
>     Le concept item pour la gestion d'inventaire des items de golf.
>     """
>     def __init__(self, nom ="", prix =""):
>         self.nom = nom
>         self.prix = prix
>
>     def set_nom(self, nom):
>         self.nom = nom
>
>     nom = property(None, set_nom)
>
>     def set_prix(self, prix):
>         self.prix = prix
>
>     prix = property(None, set_prix)
>
>     def __str__(self):
>         return self.nom
>
> class Modele:
>     """
>     La definition d'un modele avec les magasins.
>     """
>     def __init__(self, magasins =[]):
>         self.magasins = magasins
>
>     def set_magasins(self, magasins):
>         self.magasins = magasins
>
>     magasins = property(None, set_magasins)
>
>     def vide(self):
>         if self.magasins == []:
>             return True
>         else:
>             return False
>
>     def initialiser(self):
>         magasin01 = Magasin ("Swing de golf")
>
>         item01 = Item ("Ensemble de fers Titleist")
>         item01.set_prix("999.99")
>        
>         item02 = Item ("Ensemble de fers Callaway")
>
>         items = [item01, item02]
>         magasin01.set_items(items)
>
>     def afficher(self):
>         print("")
>         print("Magasins")
>         for magasin in self.magasins:
>             print("")
>             print(magasin)
>             for tache in magasin.items:
>                 print(item)
>         self.set_magasins([magasin01])
>
>
>
> if __name__ == '__main__':     
>     modele = Modele()
>     #modele.charger()
>     if modele.vide():
>         modele.initialiser()
>     modele.afficher()
>
> My error is:
>
> Magasins
>
> Traceback (most recent call last):
>   File "F:\School\University\Session 4\Programmation
> SIO\Golftmodele.py", line 102, in <module>
>     modele.afficher()
>   File "F:\School\University\Session 4\Programmation
> SIO\Golftmodele.py", line 93, in afficher
>     self.set_magasins([magasin01])
> NameError: global name 'magasin01' is not defined
>
> Thank you
> -- 
> Marc-O. Rompré

Bien,
    for starters please get rid of all those set_ methods. They are
doing nothing, it's not pythonic. Just assign the value straight away.
e.g.: from """ item01.set_prix("999.99") """  to """ item01.prix =
"999.99" """
BTW, why do you use prix as a character? Shouldn't it be a number? Check
the decimal module.
Your error comes from the fact that magasin01 only exists inside
initialiser(), it is local to this method, so afficher() can't reference it.
Note : notice the """ print(tache) """ correction. The code is untested
and probably compliant with 2.5 and not 3.x

 from decimal import Decimal
 class Magasin:
    """
    Le concept magasin pour la gestion d'inventaire des items de golf.
    """
    def __init__(self, nom ="", items =[] ):
        self.nom = nom
        self.items = items

    def __str__(self):
        return self.nom

class Item:
    """
    Le concept item pour la gestion d'inventaire des items de golf.
    """
    def __init__(self, nom ="", prix =None):
        self.nom = nom
        self.prix = prix

    def __str__(self):
        return self.nom

class Modele:
    """
    La definition d'un modele avec les magasins.
    """
    def __init__(self, magasins =[]):
        self.magasins = magasins

    def vide(self):
        return not self.magasins     # Though I can't see why you would
want this method. Just ask for the property and done

    def initialiser(self):
        items = [Item ("Ensemble de fers Titleist", Decimal("999.99")),
                     Item ("Ensemble de fers Callaway")]
        self.magasin = Magasin ("Swing de golf", items)

    def afficher(self):
        print("")
        print("Magasins")
        for magasin in self.magasins:
            print("")
            print(magasin)
            for tache in magasin.items:
                print(tache)



if __name__ == '__main__':     
    modele = Modele()
    #modele.charger()
    if modele.vide():
        modele.initialiser()
    modele.afficher()



More information about the Tutor mailing list