Jambofun
Jambofun

Reputation: 85

UnboundLocalError. Why has this occurred?

c.execute("SELECT * FROM Student, Behaviour")
data = c.fetchall() #'data' is a tuple and cannot be altered
l = []
for i in data: #for individual records in the whole database do:
    record = str(i)
    record = record.replace("u'","")
    record = record.replace("'", "")
    record = record.replace('"', '')
    record = record.replace("(","")
    record = record.replace(")", "")
    Formattedrecord = record.replace(","," -")
l.append(Formattedrecord)

This returns the error: UnboundLocalError: local variable 'Formattedrecord' referenced before assignment But I do not understand why, can somebody help?

Upvotes: 0

Views: 113

Answers (1)

Ruben Bermudez
Ruben Bermudez

Reputation: 2333

Take care with indentation:

c.execute("SELECT * FROM Student, Behaviour")
data = c.fetchall() #'data' is a tuple and cannot be altered
l = []
for i in data: #for individual records in the whole database do:
    record = str(i)
    record = record.replace("u'","")
    record = record.replace("'", "")
    record = record.replace('"', '')
    record = record.replace("(","")
    record = record.replace(")", "")
    Formattedrecord = record.replace(","," -")
    l.append(Formattedrecord) # this should be part of the for loop

Upvotes: 1

Related Questions