this is my first question in this website.
the problem is, i tried to adapt a function of python 2 to python 3.14, the function is eliminaCarta, that take a class named carta and delete this carta for a list in another class called mazo, the problem is that the function delete the first element of the list, not the correct element, i dont know where is the problem. The code contain another function, but the principal with problem is the last in the class mazo
import random
class Carta:
listaDePalos = ["Tréboles", "Diamantes", "Corazones", "Picas"]
listaDeValores = ["nada", "As", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Sota", "Reina", "Rey"]
def __init__(self, palo=0, valor=0):
self.palo = palo
self.valor = valor
def __str__(self):
return (self.listaDeValores[self.valor] + " de " + self.listaDePalos[self.palo])
def __lt__(self, other):
# controlar el palo
if self.palo > other.palo: return 1
if self.palo < other.palo: return -1
if self.valor == 1 and self.valor != other.valor: return 1
if other.valor == 1 and self.valor != other.valor: return -1
# si son del mismo palo, controlar el valor
if self.valor > other.valor: return 1
if self.valor < other.valor: return -1
# los valores son iguales, es un empate
return 0
def __gt__(self, other):
# controlar el palo
if self.palo > other.palo: return 1
if self.palo < other.palo: return -1
if self.valor == 1 and self.valor != other.valor: return 1
if other.valor == 1 and self.valor != other.valor: return -1
# si son del mismo palo, controlar el valor
if self.valor > other.valor: return 1
if self.valor < other.valor: return -1
# los valores son iguales, es un empate
return 0
def __eq__(self, other):
if self.valor == other.valor: return 1
else: return -1
class Mazo:
def __init__(self):
self.cartas = []
for palo in range(4):
for valor in range(1, 14):
self.cartas.append(Carta(palo, valor))
def muestraMazo(self):
for carta in self.cartas:
print(carta)
def __str__(self):
s = ""
for i in range(len(self.cartas)):
s = s + " "*i + str(self.cartas[i]) + "\n"
return s
def mezclar(self):
nCartas = len(self.cartas)
for i in range(nCartas):
j = random.randrange(i, nCartas)
self.cartas[i], self.cartas[j] = self.cartas[j], self.cartas[i]
def __contains__(self, item):
return item in self.items
def eliminaCarta(self, carta):
if carta in self.cartas:
self.cartas.remove(carta)
return 1
else:
return 0
carta = Carta(1,1)
mazo = Mazo()
print(mazo)
When print mazo, qe obtain
As de Tréboles
2 de Tréboles
3 de Tréboles
4 de Tréboles
5 de Tréboles
6 de Tréboles
7 de Tréboles
8 de Tréboles
9 de Tréboles
10 de Tréboles
Sota de Tréboles
Reina de Tréboles
Rey de Tréboles
As de Diamantes
2 de Diamantes
3 de Diamantes
4 de Diamantes
5 de Diamantes
6 de Diamantes
7 de Diamantes
8 de Diamantes
9 de Diamantes
10 de Diamantes
Sota de Diamantes
Reina de Diamantes
Rey de Diamantes
As de Corazones
2 de Corazones
3 de Corazones
4 de Corazones
5 de Corazones
6 de Corazones
7 de Corazones
8 de Corazones
9 de Corazones
10 de Corazones
Sota de Corazones
Reina de Corazones
Rey de Corazones
As de Picas
2 de Picas
3 de Picas
4 de Picas
5 de Picas
6 de Picas
7 de Picas
8 de Picas
9 de Picas
10 de Picas
Sota de Picas
Reina de Picas
Rey de Picas
but, when apli mazo.eliminaCarta(carta)
carta = Carta(1,13)
mazo = Mazo()
mazo.eliminaCarta(carta)
print(mazo)
the result is:
2 de Tréboles
3 de Tréboles
4 de Tréboles
5 de Tréboles
6 de Tréboles
7 de Tréboles
8 de Tréboles
9 de Tréboles
10 de Tréboles
Sota de Tréboles
Reina de Tréboles
Rey de Tréboles
As de Diamantes
2 de Diamantes
3 de Diamantes
4 de Diamantes
5 de Diamantes
6 de Diamantes
7 de Diamantes
8 de Diamantes
9 de Diamantes
10 de Diamantes
Sota de Diamantes
Reina de Diamantes
Rey de Diamantes
As de Corazones
2 de Corazones
3 de Corazones
4 de Corazones
5 de Corazones
6 de Corazones
7 de Corazones
8 de Corazones
9 de Corazones
10 de Corazones
Sota de Corazones
Reina de Corazones
Rey de Corazones
As de Picas
2 de Picas
3 de Picas
4 de Picas
5 de Picas
6 de Picas
7 de Picas
8 de Picas
9 de Picas
10 de Picas
Sota de Picas
Reina de Picas
Rey de Picas
the first element is delete, when the element that must be delete is "Rey de Diamantes"