Unboundlocalerror: local variable '' referenced before assignment python

Asked

Viewed 573 times

-1

I’m having trouble passing the name changed if it comes to a certain value CODE:

import threading
import time
from random import randint

def worker(message):
    f = open('addresses_0.csv', 'r')
    inicio = time.time()
    contador = 0
    for line in f:
        if contador >= 100000:
             lista = randint(0,9)
             return
        else:
            separa = line.split(',')
            dd = separa[0].strip().replace('"','')
            numero = separa[1].strip().replace('"','')
            print(lista)
            aprovadas = open('lista'+lista+'.txt', 'a')
            aprovadas.write('%s|%s\n' %(dd,numero))
            aprovadas.close()
            contador = contador+1

t = threading.Thread(target=worker,args=("thread sendo executada5",))
t.start()

1 answer

0


well first you set the variable within the if It is this attempt used inside else plus the else cannot generate the variable inside the if It’s just you take out the variable from inside the if

is no need of Else proque if it reaches the if it will return false and will not run the next lines hope to have helping

import threading
import time
from random import randint

def worker(message):
    f = open('addresses_0.csv', 'r')
    inicio = time.time()
    contador = 0
    for line in f:
        lista = randint(0,9) <----
        if contador >= 100000:
             return False
        
        separa = line.split(',')
        dd = separa[0].strip().replace('"','')
        numero = separa[1].strip().replace('"','')
        print(lista)
        aprovadas = open('lista'+lista+'.txt', 'a')
        aprovadas.write('%s|%s\n' %(dd,numero))
        aprovadas.close()
        contador = contador+1

t = threading.Thread(target=worker,args=("thread sendo executada5",))
t.start()

Browser other questions tagged

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