本地变量“服务器”在分配之前引用 [英] local variable 'servers' referenced before assignment
问题描述
db = MySQLdb.connect(localhost,root,aqw,PFE_Project)
cursor = db.cursor()
sql =SELECT * FROM myform_composantsserveur
try:
cursor.execute(sql)
results = cursor.fetchall()
currency_in = currency
req = urllib2.urlopen('http://rate-exchange.appspot.com/currency?from=USD&to=%s')%(currency_in)
req1 = req.read()
rate = int(req1 ['rate'])
#rate = 0.77112893299999996
servers = []
for results in results:
result = {}
result ['1'] = row [1]
result ['3'] = int(row [2])
result ['4'] = int(row [3])
result ['5'] = int(row [4])
result ['6'] = row [5]
result ['7' ] = int(row [6])
result ['8'] = row [7]
result ['9'] = row [8]
p = rate * calculate_metric(int(row [2]),int(row [3]),int(row [4]),int(row [6]),row [ )
result ['2'] = p
keys = result.keys()
keys.sort()
servers.append(result)
除了:
打印错误:无法获取数据
db.close()
返回服务器
但编译代码时我有这个错误:
异常类型:UnboundLocalError
异常值:局部变量
'服务器在分配前引用
异常位置:/ home / amine / PFE目录/ mysite1 / myform / Webservice.py在websvc,第43行Python可执行文件:/usr/bin/python2.7
此代码在这个函数中添加参数之前,正常工作
您的代码无法到达服务器启动这就是为什么你得到错误。只需在try..except之前移动初始化。改变这种方式:
def websvc(currency):
pre>
db = MySQLdb.connect(localhost,root ,aqw,PFE_Project)
cursor = db.cursor()
sql =SELECT * FROM myform_composantsserveur
servers = []
try :
cursor.execute(sql)
results = cursor.fetchall()
currency_in = currency
req = urllib2.urlopen('http://rate-exchange.appspot。 com / currency?from = USD& to =%s')%(currency_in)
req1 = req.read()
rate = int(req1 ['rate'])
#rate = 0.77112893299999996
结果中的行:
result = {}
result ['1'] = row [1]
result ['3'] = int (row [2])
result ['4'] = int(row [3])
result ['5'] = int(row [4])
result ['6 '] = row [5]
result ['7'] = int(row [6])
result ['8'] = row [7]
结果['9'] =行[8]
p = rate * calculate_metric(int(row [2]),int(row [3]),int(row [4]),int(row [6] ),行[7])
result ['2'] = p
keys = result.keys()
keys.sort()
servers.append(result)
除了:
打印错误:无法获取数据
db.close()
返回服务器
def websvc(currency): db = MySQLdb.connect("localhost", "root", "aqw", "PFE_Project") cursor = db.cursor() sql = "SELECT * FROM myform_composantsserveur" try: cursor.execute(sql) results = cursor.fetchall() currency_in = currency req = urllib2.urlopen('http://rate-exchange.appspot.com/currency?from=USD&to=%s') % (currency_in) req1 = req.read() rate = int(req1['rate']) # rate = 0.77112893299999996 servers = [] for row in results: result = {} result['1'] = row[1] result['3'] = int(row[2]) result['4'] = int(row[3]) result['5'] = int(row[4]) result['6'] = row[5] result['7'] = int(row[6]) result['8'] = row[7] result['9'] = row[8] p = rate * calculations_metric (int(row[2]), int(row[3]), int(row[4]), int(row[6]), row[7]) result['2'] = p keys = result.keys() keys.sort() servers.append(result) except: print "Error: unable to fetch data" db.close() return servers
but i have this error while compiling the code :
Exception Type: UnboundLocalError
Exception Value: local variable 'servers' referenced before assignment
Exception Location: /home/amine/PFE Directory/mysite1/myform/Webservice.py in websvc, line 43 Python Executable: /usr/bin/python2.7
this code works normally before i added a parameter in this function
解决方案Your code not able to reach servers initialization and that is why you getting error. Simply move initialization before try..except. Change this way:
def websvc(currency): db = MySQLdb.connect("localhost", "root", "aqw", "PFE_Project") cursor = db.cursor() sql = "SELECT * FROM myform_composantsserveur" servers = [] try: cursor.execute(sql) results = cursor.fetchall() currency_in = currency req = urllib2.urlopen('http://rate-exchange.appspot.com/currency?from=USD&to=%s') % (currency_in) req1 = req.read() rate = int(req1['rate']) # rate = 0.77112893299999996 for row in results: result = {} result['1'] = row[1] result['3'] = int(row[2]) result['4'] = int(row[3]) result['5'] = int(row[4]) result['6'] = row[5] result['7'] = int(row[6]) result['8'] = row[7] result['9'] = row[8] p = rate * calculations_metric (int(row[2]), int(row[3]), int(row[4]), int(row[6]), row[7]) result['2'] = p keys = result.keys() keys.sort() servers.append(result) except: print "Error: unable to fetch data" db.close() return servers
这篇关于本地变量“服务器”在分配之前引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!