Heretic Curse
01-05-2013, 12:01
>>>class Carta:
... ListaSemi = ["Fiori", "Quadri", "Cuori", "Picche"]
... ListaRanghi = ["impossibile", "Asso", "2", "3", "4", "5", \
... "6", "7", "8", "9", "10", "Jack", "Regina", "Re"]
... def __init__(self, Seme=0, Rango=0):
... self.Seme = Seme
... self.Rango = Rango
... def __str__(self):
... return (self.ListaRanghi[self.Rango] + " di " + self.ListaSemi[self.Seme])
...
... def __cmp__(self, Altro):
... if self.Seme > Altro.Seme: return 1
... if self.Seme < Altro.Seme: return -1
...
... if self.Rango == ListaRanghi[1] and Altro.Rango != ListaRanghi[1]: return 1
... if self.Rango > Altro.Rango: return 1
... if self.Rango < Altro.Rango: return -1
...
... return 0
>>>F = Carta(1, 1)
>>>T = Carta(1, 13)
>>>F.__cmp__(T)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 15, in __cmp__
NameError: global name 'ListaRanghi' is not defined
non riesco a capire il perché di quell'errore. Dovrei per caso specificare le 2 liste all'interno di __cmp__? Non credo, per __str__ (che funziona) non è stato necessario :doh:
... ListaSemi = ["Fiori", "Quadri", "Cuori", "Picche"]
... ListaRanghi = ["impossibile", "Asso", "2", "3", "4", "5", \
... "6", "7", "8", "9", "10", "Jack", "Regina", "Re"]
... def __init__(self, Seme=0, Rango=0):
... self.Seme = Seme
... self.Rango = Rango
... def __str__(self):
... return (self.ListaRanghi[self.Rango] + " di " + self.ListaSemi[self.Seme])
...
... def __cmp__(self, Altro):
... if self.Seme > Altro.Seme: return 1
... if self.Seme < Altro.Seme: return -1
...
... if self.Rango == ListaRanghi[1] and Altro.Rango != ListaRanghi[1]: return 1
... if self.Rango > Altro.Rango: return 1
... if self.Rango < Altro.Rango: return -1
...
... return 0
>>>F = Carta(1, 1)
>>>T = Carta(1, 13)
>>>F.__cmp__(T)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 15, in __cmp__
NameError: global name 'ListaRanghi' is not defined
non riesco a capire il perché di quell'errore. Dovrei per caso specificare le 2 liste all'interno di __cmp__? Non credo, per __str__ (che funziona) non è stato necessario :doh: