【问题标题】:How to stop a variable from being referenced without removing it?如何在不删除变量的情况下阻止变量被引用?
【发布时间】:2019-06-09 06:28:34
【问题描述】:

因此,在我进行编程时,我创建了一个变量 (rYouTube),用于存储识别器。我创建了另一个名为 rGoogle 的变量,其中存储了另一个识别器。唯一的问题是,每次我选择 Google 而不是 YouTube 时,我都会不断收到错误消息“UnboundLocalError: local variable 'rYouTube' referenced before assignment”,因为我的程序的工作方式是你选择一个然后程序继续运行(如果你选择了 YouTube ,你可以看看东西,如果你选择谷歌,你可以查东西)

所以我已经尝试将变量值作为占位符提供,但由于这些变量是音频变量,它不起作用。

                print("Would you like to Direct Search?")
            rYouTube = sr.Recognizer()
            with sr.Microphone() as source:
                        rYouTube.adjust_for_ambient_noise(source)
                        YTaudio = rYouTube.listen(source)
                        print("LOADING...")
                        time.sleep(1)

        try:
                DirectYTRecognized = rYouTube.recognize_google(YTaudio)
                print(DirectYTRecognized)
        except sr.UnknownValueError:
                print("Google Speech Recognition could not understand audio")
        except sr.RequestError as e:
                print("Could not request results from Google Speech Recognition service; {0}".format(e))


        if "yes" in DirectYTRecognized:
                print("What do you want to watch?")
        SearchYouTube = sr.Recognizer()
        with sr.Microphone() as source:
                        SearchYouTube.adjust_for_ambient_noise(source)
                        YTSearchAudio = SearchYouTube.listen(source)
                        print("LOADING...")
                        time.sleep(1)

        try:
                FinalSearchYTAudio = SearchYouTube.recognize_google(YTSearchAudio)
                print(FinalSearchYTAudio)
            DirectYT = "https://youtube.com/results?search_query=" + FinalSearchYTAudio
            webbrowser.open_new(DirectYT)
        except sr.UnknownValueError:
                print("Google Speech Recognition could not understand audio")
        except sr.RequestError as e:
                print("Could not request results from Google Speech Recognition service; {0}".format(e))

#END OF YT DIRECT SEARCH-------------------------------------

#GOOGLE DIRECT SEARCH---------------------------------------
        if "Google" in recognized:
                print("Would you like to Direct Search?")
            rGoogle = sr.Recognizer()
            with sr.Microphone() as source:
                        rGoogle.adjust_for_ambient_noise(source)
                        GoogleAudio = rGoogle.listen(source)
                        print("LOADING...")
                        time.sleep(1)

        try:
                DirectGoogleRecognized = rGoogle.recognize_google(GoogleAudio)
                print(DirectGoogleRecognized)
        except sr.UnknownValueError:
                print("Google Speech Recognition could not understand audio")
        except sr.RequestError as e:
                print("Could not request results from Google Speech Recognition service; {0}".format(e))


        if "yes" in DirectGoogleRecognized:
                print("What do you want to look up?")
        SearchGoogle = sr.Recognizer()
        with sr.Microphone() as source:
                        SearchGoogle.adjust_for_ambient_noise(source)
                        GoogleSearchAudio = SearchGoogle.listen(source)
                        print("LOADING...")
                        time.sleep(1)

        try:
                FinalSearchGooleAudio = SearchGoogle.recognize_google(YTSearchAudio)
                print(FinalSearchGoogleAudio)
            DirectGoogle = "https://youtube.com/results?search_query=" + FinalSearchGoogleAudio
            webbrowser.open_new(DirectGoogle)
        except sr.UnknownValueError:
                print("Google Speech Recognition could not understand audio")
        except sr.RequestError as e:
                print("Could not request results from Google Speech Recognition service; {0}".format(e))

我希望程序继续工作,但它停止并显示:“UnboundLocalError: local variable 'rYouTube' referenced before assignment”

【问题讨论】:

    标签: python variables speech-recognition


    【解决方案1】:

    你的 Try except 语句不应该尽可能地缩进,它们应该缩进到与它们之前的 with 相同的级别。 (这似乎是您发布的代码及其显示的格式的问题。

    【讨论】:

    • 它们之所以被淘汰是因为它们是函数的一部分,我没有在代码中包含它,抱歉
    • 在您的代码中,返回未绑定的变量由嵌套的块分配。 try 块不在同一个范围内(它在外部范围内,变量尚未设置为任何内容。就变量范围而言,缩进很重要。
    猜你喜欢
    • 2020-11-29
    • 2018-03-27
    • 1970-01-01
    • 2012-09-11
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 2014-11-11
    • 1970-01-01
    相关资源
    最近更新 更多