【问题标题】:I don't know whats wrong with the below cod, thrrows an error "UnboundLocalError: local variable 'r' referenced before assignment"我不知道下面的 cod 有什么问题,抛出错误“UnboundLocalError: local variable 'r' referenced before assignment”
【发布时间】:2018-01-22 02:47:06
【问题描述】:

我不知道下面的代码有什么问题,抛出一个错误,

UnboundLocalError: local variable 'r' referenced before assignment

请帮帮我

代码:

if i != 0:
    agentAttributes = etree.SubElement(root, "agentAttributes")
    agentAttribute = etree.SubElement(agentAttributes, "agentAttribute")
    attribute = etree.SubElement(agentAttribute, "attribute")
    refURL = etree.SubElement(attribute, "refURL")
    refURL.text = "/unifiedconfig/config/attribute/5007"
    name = etree.SubElement(attribute, "name")
    name.text = "CustomerService"
    attributeValue = etree.SubElement(agentAttribute, "attributeValue")
    attributeValue.text = "5"
    datatype = etree.SubElement(attribute, "datatype")
    datatype.text = "4"

    # print("After adding"+(str(etree.tostring(root))))


    r = requests.put(wsurl, verify=False, data=(etree.tostring(root)),
                     headers={"Content-Type": "application/xml"}, auth=('tsup01', 'Sp33chs0ft'))
    #       print("after put"+(r.text))
conn.close()
conn1.close()

#print("Adding one Attribute to all agents duration in hh:mm:ss  = " + str(datetime.now() - startTime))
#returnString = "Adding one Attribute to all agents duration in hh:mm:ss  = " + str(datetime.now() - startTime)
return HttpResponse(r.text)

【问题讨论】:

  • 您能提供完整的堆栈跟踪吗?还有为什么你有一个sails.js 标签?谢谢!

标签: python mysql sails.js


【解决方案1】:

您的问题来自rif i != 0: 语句中定义。由于您没有提供完整的功能,我假设您没有在if 语句之前分配变量r,这意味着当您执行return HttpResponse(r.text) 时,r 确实未分配!

要解决您的问题,请在 if i != 0: 语句之外分配 r

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2021-05-11
    • 2017-08-08
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    • 2023-02-13
    • 2013-02-11
    • 2021-12-17
    • 1970-01-01
    相关资源
    最近更新 更多