简体   繁体   中英

I keep getting this error local variable 'url' referenced before assignment

It doesnt run the loop

Changed the variable url name and still didnt work

@

app.route("/processbuy", methods=["POST"])
def processbuy():
 if request.method == "POST":
        index2 = request.form['index2']
        querystring = {"api_key":"NN2T8jrqC6UH5inDezHh"}
        payload = ""
        headers = {
            'cache-control': "no-cache",
            'Postman-Token': "d2cd69a4-e6d4-466c-88b3-0a3987b1cd7d"
        }                              
        print("dsad")
        if index2 == "Stock Market Index at Exchange: NYSE":
                url = "https://www.quandl.com/api/v3/datasets/WFE/INDEXES_NYSE.json"
                response = requests.request("GET", url, data=payload, headers=headers, params=querystring)
                indiceData = json.loads(response.text)
                latestIndicePrices = indiceData["dataset"]["data"][0]
                indexValue = float(latestIndicePrices[5])

        else: 
                if index2 == "NASDAQ-100 Target 25 Notional Net Return Index(XNDXT25NNR)":
                    url = "https://www.quandl.com/api/v3/datasets/NASDAQOMX/XNDXT25NNR.json"
                elif index2 == "Stock Market Index at Exchange: London Stock Exchange FTSE 100":
                    url = "https://www.quandl.com/api/v3/datasets/WFE/INDEXES_LONDONSEFTSE.json"
                elif index2 == "Stock Market Index at Exchange: NYSE":
                    url = "https://www.quandl.com/api/v3/datasets/WFE/INDEXES_NYSE.json"
                response = requests.request("GET", url, data=payload, headers=headers, params=querystring)
                print("response="+response.text)
                indiceData = json.loads(response.text)
                latestIndicePrices = indiceData["dataset"]["data"][0]
                indexValue = float(latestIndicePrices[1])
        print("dwwsw")
        token = session['oauth_token']
        customersAccount = session['fidor_customer']
        customerDetails = customersAccount['data'][0]

When added a print after the loop it suppose to show the print but it didnt for me.

只有将index2定义为某变量时,才会初始化变量url

Your problem is the variable url might never be assigned any value. The reason for that is your if conditions.

if condition1:
    url = "something"
else:
    if condition2:
        url = "something"
    elif condition3:
        url = "something"
    elif condition4:
        url = "something"
    else: # hypothetical
        url is undefined here # and here is your issue

You can solve this by adding that last else catch or by initializing url to a default value before you check for any condition.

If you use a good IDE like Pycharm it would give you warning about these issues even before you run the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM