Problem using if and elif Python

0

I'm having a problem using if and elif to create an extract table by taking bank information.

If the transaction category is a deposit in the bank, I want it print in a way, but after it has passed the first if , if it has any other item that falls into this condition, it is not receiving the proposed conditions of if that I created.

Follow the code and return example on the screen.

idtransacao = cursor.execute('SELECT id_transacao FROM extrato WHERE id_extrato = ?',(vnconta,))
for f in idtransacao:
    x = f
    x = str(x).strip("(,)")
    listaid.append(x)
ctdr = len(listaid)
contador = 0
print('''
Codigo da transacao |   Categoria   | Destino  |   Valor    |     Data     |
------------------- |-------------- |--------- | ---------- | ------------ |''')
while contador != ctdr:
    if listacat[contador] == 'DEPOSITO':
        if len(listaval[contador]) <= 3:
            print(' '*12 + '{}'.format(listaid[contador])+' '*11+'|'+' '*2 + '{}'.format(listacat[contador])+' '*5 + '|' + ' '*2 + '----' +' '*4+ '|' + ' '*2 + '{}'.format(listaval[contador] + ' '*7 + '|' + ' '*2 + '{}'.format(listadate[contador] + ' '*2 + '|')))
            contador += 1
        elif len(listaval[contador]) <= 2:
            print(' '*12 + '{}'.format(listaid[contador])+' '*11+'|'+' '*2 + '{}'.format(listacat[contador])+' '*5 + '|' + ' '*2 + '----' +' '*4+ '|' + ' '*2 + '{}'.format(listaval[contador] + ' '*8 + '|' + ' '*2 + '{}'.format(listadate[contador] + ' '*2 + '|')))     
    elif listacat[contador] == 'SAQUE':
        print(' '*12 + '{}'.format(listaid[contador])+' '*11+'|'+' '*2 + '{}'.format(listacat[contador])+' '*8 + '|' + ' '*2 + '----' +' '*4+ '|' + ' '*2 + '{}'.format(listaval[contador] + ' '*7 + '|' + ' '*2 + '{}'.format(listadate[contador] + ' '*2 + '|')))
        contador += 1
    elif listacat[contador] == 'TRANSFERENCIA':
        print(' '*12 + '{}'.format(listaid[contador])+' '*11+'|'+' '*2 + '{}'.format(listacat[contador]) + '|' + ' '*4 + '{}'.format(listadet[contador]) +' '*5+ '|' + ' '*2 + '{}'.format(listaval[contador] + ' '*7 + '|' + ' '*2 + '{}'.format(listadate[contador] + ' '*2 + '|')))
        contador += 1   
===============================================================================
Codigo da transacao |   Categoria   | Destino  |   Valor    |     Data     |
------------------- |-------------- |--------- | ---------- | ------------ |
        1           |  DEPOSITO     |  ----    |  100       |  2018-12-06  |
        2           |  SAQUE        |  ----    |  200       |  2018-12-11  |
        3           |  DEPOSITO     |  ----    |  600       |  2018-12-06  |
        4           |  SAQUE        |  ----    |  200       |  2018-12-11  |
        5           |  TRANSFERENCIA|    2     |  10       |  2018-12-06  |
        6           |  DEPOSITO     |  ----    |  10       |  2018-12-06  | <<<- deveria pegar o if igual ao codigo da transacao '1'
    
asked by anonymous 13.12.2018 / 16:20

1 answer

0

It seems to me that you are changing the 'counter' variable before using it entirely. See inside the loop, after the second 'if' you change 'counter'. In the next line you use 'listaval [counter]' and now you are no longer using the same information as before because the counter has been incremented.

I'm not going to suggest a change to your code directly, because you can not tell how it has to work in the end. It seems to me, however, that you would either need two counters or leave it to increment it at the end of the 'while'.

    
13.12.2018 / 22:56