Problem using if and Elif Python

Asked

Viewed 115 times

0

I have a problem using the if and the elif to create an extract table taking information from the bank.

If in the bank the transaction category is deposit, I want it print in a way, but after he passed the first if, if you have any other items that fit this condition, it is not receiving the proposed conditions of if that I created.

Follow the example of code and return 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'
  • For those of you who are out of context was a little confused your question, da para vc dar um print(listaid) and show the result in the question?

1 answer

0


It seems to me that you are changing the variable 'counter' before using it entirely. See that 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’t tell how it has to work at the end. It seems to me, however, that you would need either two counters or leave to increment it at the end of the 'while''.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.