关于python:在赋值之前引用的局部变量’servers’

local variable 'servers' referenced before assignment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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

但在编译代码时出现了以下错误:

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

在我在函数中添加参数之前,此代码正常工作


您的代码无法到达服务器初始化,这就是出现错误的原因。只需在尝试..之前移动初始化。更改方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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

我看到问题了,现在你已经编辑它来添加缺失的部分。它是异常处理程序。

如果在try之后和servers=[]之前有错误,它将跳转到except子句,然后看到return servers并失败。

您可能希望使用list(),而不是使用dict()来模拟列表…