I created two Python files, one class and one Main and wanted to compare two instances to see who had the most value
class Conta:
def __init__(self, titular, numero, quantidade):
self.titular = titular
self.numero = numero
self.quantidade = quantidade
def info(self):
print(f"The Account {self.numero} with the titular {self.titular} has {self.quantidade}")
from Conta import Conta
Conta1 = Conta("Miguel", "987654", "300")
Conta2 = Conta("Fernando", "123456", "600")
Conta1.info()
Conta2.info()
valor = Conta1.quantidade - Conta2.quantidade
if Conta1.quantidade <= Conta2.quantidade:
print(f"O {Conta1.titular} has more {valor} then {Conta2.titular}")
elif Conta2.quantidade <= Conta1.quantidade:
print(f"O {Conta2.titular} has more {valor} then {Conta1.titular}")
elif Conta1.quantidade == Conta2.quantidade:
print(f"O {Conta1.titular} has same value then {Conta2.titular}")
But I can’t seem to get a valor for the difference between the two instances.
Fire_Elite YT is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
6
You have one obvious issue, which is that your quantidade
attribute is a string. If you cast that to an int, those attributes can be compared directly:
class Conta:
def __init__(self, titular, numero, quantidade):
self.titular = titular
self.numero = numero
self.quantidade = int(quantidade)
1