Python Unbound Local变量

时间:2017-12-12 01:19:39

标签: python

为什么我的check_email错误,我不知道如何解决它

def getLoginDetails():
    with sqlite3.connect('database.db') as conn:
        cur = conn.cursor()
        if 'email' not in session:
            loggedIn = False
            firstName = ''
            noOfItems = 0
        else:
            loggedIn = True
            cur.execute("SELECT userId, firstName FROM users WHERE email = '" + session['email'] + "'")
            userId, firstName = cur.fetchone()
            if 'email' == "admin@shop.com":
                check_email = True
            else:
                check_email = False
            cur.execute("SELECT count(productId) FROM kart WHERE userId = " + str(userId))
            noOfItems = cur.fetchone()[0]
    conn.close()
    return (loggedIn, firstName, noOfItems, check_email)

@app.route("/")
def root():
    loggedIn, firstName, noOfItems, check_email = getLoginDetails()
    with sqlite3.connect('database.db') as conn:
        cur = conn.cursor()
        cur.execute('SELECT productId, name, price, description, image, stock FROM products')
        itemData = cur.fetchall()
        cur.execute('SELECT categoryId, name FROM categories')
        categoryData = cur.fetchall()
    itemData = parse(itemData)
    return render_template('home.html', itemData=itemData, loggedIn=loggedIn, firstName=firstName, noOfItems=noOfItems, categoryData=categoryData, check_email=check_email)

这对我来说毫无意义,因为我清楚地将check_email初始化为我的代码的第一行,我甚至将其标记为全局只是为了安全并确保它在我所有方法的范围内。

错误:UnboundLocalError:本地变量' check_email'在分配前引用

1 个答案:

答案 0 :(得分:0)

如果check_email,您不会分配到'email' not in session