如何在不删除变量的情况下停止对其进行引用?

时间:2019-06-08 21:50:17

标签: python variables speech-recognition

因此,在编程时,我创建了一个变量(rYouTube),用于存储Recognizer。我创建了另一个名为rGoogle的变量,在其中存储了另一个Recognizer。唯一的问题是,每次选择Google而不是YouTube时,我都会不断收到错误消息“ UnboundLocalError:本地变量'rYouTube'在分配前被引用”,因为我的程序的工作方式是选择一个然后程序继续运行(如果选择YouTube, ,您可以查看手表内容,如果您选择了Google,则可以查找内容)

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

                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:分配前引用了本地变量'rYouTube'”

1 个答案:

答案 0 :(得分:1)

您的Try Except语句不应被缩进到尽可能远的位置,它们应该缩进到与它们之前的语句相同的级别。 (这就是您发布的代码所显示的格式问题。