I have a bug in my application. I did an if that if the must is greater than the credit, do such a thing, and the else the opposite. I put an item, I put the debit 500 and the credit 4000 and it takes the first if, but if I put 600 and not 8000, it goes to the else.
def add_product(self):
try:
if (rows:= self.listbox1.selection_get()):
if rows in RESULTADOS:
if self.price_debe.get() > self.price_haber.get():
negativo_a = (int(self.price_debe.get()) - int(self.price_haber.get()))
query = 'INSERT INTO balance (id, cuenta, debe, haber, activo, pasivo, negativo, positivo) VALUES (?, ?, ?, ?, ?, ?, ?, ?)'
parameters = (self.nro_cuenta.get(), rows, self.price_debe.get(), self.price_haber.get(), 0, 0, negativo_a, 0)
self.run_query(query, parameters)
self.pasivo_negativo(self.nro_cuenta.get())
self.mesagge['text'] = 'Se guardo el resultado negativo'
else:
positivo_a = (int(self.price_haber.get()) - int(self.price_debe.get()))
query = 'INSERT INTO balance (id, cuenta, debe, haber, activo, pasivo, negativo, positivo) VALUES (?, ?, ?, ?, ?, ?, ?, ?)'
parameters = (self.nro_cuenta.get(), rows, self.price_debe.get(), self.price_haber.get(), 0, 0, 0, positivo_a)
self.run_query(query, parameters)
self.pasivo_negativo(self.nro_cuenta.get())
self.mesagge['text'] = 'Se guardo el resultado positivo'
else:
query = 'INSERT INTO balance (id, cuenta, debe, haber) VALUES (?, ?, ?, ?)'
parameters = (self.nro_cuenta.get(), rows, self.price_debe.get(), self.price_haber.get())
self.run_query(query, parameters)
self.price_debe.delete(0, END)
self.price_debe.insert(0, int(0))
self.price_haber.delete(0, END)
self.price_haber.insert(0, int(0))
self.nro_cuenta.delete(0, END)
self.mesagge['text'] = 'Se guardo con exito'
else:
self.mesagge['text'] = 'Hay un error'
except (sqlite3.IntegrityError, TclError):
self.mesagge['text'] = 'No hay datos o ya esta cargado'
A ListBox stores strings, so you're comparing strings, not integers. This means that they are sorted and compared in lexicographical order, i.e. based on the order of their Unicode codepoints:
When comparing by lexicographical order, first the first character of both is looked at, in the first case it is "5" and "4", since "5" is greater than "4" True is immediately returned. In the second case, since "6" is less than "8", "False" is returned. If both characters are equal, the second is compared identically, etc.
This leads to "2" being greater than "10000000000000000000000000000". The solution is simply to cast to integer or
float
(depending on whether or not you have decimals) previously:In your case it would be:
or if you are going to work with floats: