Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何解决telethon UnboundLocalError_Python_Variables_Telethon - Fatal编程技术网

Python 如何解决telethon UnboundLocalError

Python 如何解决telethon UnboundLocalError,python,variables,telethon,Python,Variables,Telethon,我在代码的最后一行得到这个错误。如果有人遇到过同样的问题,我很乐意与大家分享如何解决。 源代码是基于telethon的,并且是完整的。执行也是成功的,但是当想要对userid做出响应时,它会给出UnboundLocalError 代码: @client.on(events.NewMessage(incoming=True, from_users=(723428565, 677543378))) async def _(event): if event.fwd_from:

我在代码的最后一行得到这个错误。如果有人遇到过同样的问题,我很乐意与大家分享如何解决。 源代码是基于telethon的,并且是完整的。执行也是成功的,但是当想要对userid做出响应时,它会给出UnboundLocalError

代码:

@client.on(events.NewMessage(incoming=True, from_users=(723428565, 677543378)))
async def _(event):
    if event.fwd_from:
        return
    url = "http://www.google.com" 
    if event.reply_to_msg_id and "allow" in event.raw_text:
        previous_message = await event.get_reply_message()
        previous_message_text = previous_message.message
        if previous_message.media:
            downloaded_file_name = await client.download_media(
                previous_message,
                path, 
            )
            surl = "{}/searchbyimage/upload".format(url)
            multipart = {
                "encoded_image": (
                    downloaded_file_name,
                    open(downloaded_file_name, "rb"),
                ),
                "image_content": "",
            }
            google_rs_response = requests.post(
                surl, files=multipart, allow_redirects=False
            )
            the_location = google_rs_response.headers.get("Location")
            os.remove(downloaded_file_name)
        else:
            previous_message_text = previous_message.message
            surl = "{}/searchbyimage?image_url={}"
            request_url = surl.format(url, previous_message_text)
            google_rs_response = requests.get(request_url, allow_redirects=False)
            the_location = google_rs_response.headers.get("Location")
        headers = {
            "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0"
        }
        response = requests.get(the_location, headers=headers)
        soup = BeautifulSoup(response.text, "html.parser")
        bro = soup.find_all("div", {"class": "r5a77d"})[0]
        lol = bro.find("a")
        url + lol.get("href")
        final = lol.text
    await event.edit(
        event.chat_id, final.replace("me", "")
    ) 
错误:

Line 42: UnboundLocalError: local variable 'final' referenced before assignment

您正在事件中的if块
if“allow”内定义变量
text=lol.text
。原始文本:


因此,看起来您的条件没有得到满足,而且变量
text
从未定义过。因此,当您试图访问它时,
wait event.edit(event.chat\u id,text.replace(“我”,“我”)
出现错误

请包含完整的堆栈跟踪。从堆栈跟踪中查看哪一行导致错误要比在分配变量之前搜索代码以查找引用变量
text
的位置容易得多,速度也快得多。请发布一条消息,让人们可以运行它来重现您的代码。请同时阅读并阅读其中的相关链接。这也很有帮助。欢迎来到堆栈溢出!另外,请不要删除并重新发布同一个…为了避免误导,我将变量名编辑为final。请看,这是bot中的一个功能,我希望它仅在指定的userid消息中存在单词“allow”时才返回答案。所以那一定是在if块中。同样对于最后一行,它在块外,如果我将它放在块内,则不会返回响应,也不会显示错误。我尝试了您所说的,但再次给出错误。如果你对如何解决这个问题有任何想法,请用所提到的错误代码部分的更正形式清楚地说出来。非常感谢你的帮助。