local variable is not being changed? python jupyter

Asked

Viewed 22 times

-1

I have a var local that for some reason is not being zeroed

print(pf,qcx)    <------ essa linha é executada
qcx = 0          <------ essa não

i do not know debuggar in jupyter and do not understand why this variable is never zeroed

below is the summary of my code:

def names(resp = 'n'):
    vprod,icms,desc,qtrib,nome,pf,x,utrib,bo,new,qcx,itens = 0,0,0,0,'',0,0,'',False,'',0,{}
    for child in rooti.iter():
        bo = False
        try:
            ...
            if bo:
                if resp == 'n':
                    for i in utrib:
                        if i.isdigit():
                            new += i
                        else:
                            pass
                    qcx = int(new)
                    pf = (vprod + icms - desc)/(qcx * qtrib)
                    print(pf,qcx)
                    qcx = 0
                else:
                    pf = (vprod + icms - desc)/qtrib
                pv = '{:.2f}'.format(pf)
                itens[nome]['preço'] = pv
            if child.tag[36:] == 'total':
                break
        except ZeroDivisionError or IndexError or KeyError:
            continue    
            

def preco():
   ...
            
def init(resp='s'):
    tree = ET.parse(xml)
    rooti = tree.getroot()
    names(resp)
    preco()
    newteste = ''   
    for i in teste:
        newteste += str(i)
print(newteste)
print('='*58)
    
    
init()
resp = input('bateu? [S/N]')
if resp.lower() == 'n':
    init('n')
print(newteste)
print('='*58)

output:

print(pf,qcx):
4.09 24
0.02410214168039539 2428
0.00024099362511736705 242828
2.5820713648285644e-06 24282830
2.589072170691088e-08 2428283024
4.632903120482823e-11 242828302415
4.632903120479961e-13 24282830241515
4.632903120479932e-15 2428283024151515
4.632903120479932e-17 242828302415151515
4.632903120479932e-19 24282830241515151515
4.632903120479932e-21 2428283024151515151515
4.6329031204799314e-23 242828302415151515151515
  • I didn’t stop to understand all the code, but anyway, before the print you do qcx = int(new), that is, the variable qcx receives some value, you print, then changes the value to zero, but then you do qcx = int(new) again, prints, switches to zero, etc... This means that the qcx = 0 becomes useless, because before printing you always change this value to int(new)

  • qcx serves inside a mathematical expression and must be reset as soon as it is executed. the problem is that qcx = 0 is not assigning 0.

  • It is assigning yes, the problem is that in the following iteration you do qcx = int(new) before using the variable again (from what I understood from your code). Put a print(qcx) shortly after qcx = 0 to confer

  • got it, int(new) was not being. obg

No answers

Browser other questions tagged

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