为什么我收到这个关于我已经定义的变量的错误信息?[重复]

问题描述 投票:-1回答:1

我正在用python做一个虚拟助手,我的一个变量给出了一个错误信息,说它是未定义的,尽管我在我的代码顶部定义了它.我不会完全详细介绍我目前的情况。我有一个名为 "responseing "的bool变量,并且在我的代码顶部将其设置为 "true"。

import pyttsx3
import datetime
import speech_recognition as sr
import wikipedia
import webbrowser
import os
import random
import sys
import pyowm
import time
import wolframalpha
import jokes_library

listening = True
responding = True

# wake_phrases = ["hello ava", "hello", "good morning", "good afternoon", "good evening", "good morning ava", "good afternoon ava", "good evening ava"]
# wake_phrase = wake_phrases[0] or wake_phrases[1] or wake_phrases[2] or wake_phrases[3] or wake_phrases[4] or wake_phrases[5] or wake_phrases[6] or wake_phrases[7]

wake_phrase = "hello ava"
call_phrase = "hey ava"

respect_words = ["sir", "chief"]
respect_word = random.choice(respect_words)

goodbye_phrases = ["my pleasure", "until tomorrow", "ok, take it easy", "you betcha", "no problemo, see you later"]
goodbye_phrase = random.choice(goodbye_phrases)

engine = pyttsx3.init("sapi5")
voices = engine.getProperty("voices")
engine.setProperty("voice", voices[1].id)

def speak(audio):
   engine.say(audio)
   engine.runAndWait() 

def wish_me():
    hour = int(datetime.datetime.now().hour)
    if hour >= 0 and hour < 12:
        speak(f"good morning {respect_word}")

    elif hour >= 12 and hour < 18:
        speak(f"good afternoon {respect_word}")

    else:
        speak(f"good evening {respect_word}")

    speak(f"how may i help you")

def listen():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        audio = r.listen(source)
    query = r.recognize_google(audio, key=None, language="en-US", show_all=False)

def take_command():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("i am listening")
        r.pause_threshold = 1
        audio = r.listen(source)

    try:
        print("recognizing")
        query = r.recognize_google(audio, key=None, language="en-US", show_all=False)
        print(f"user said: {query}\n")

    except Exception as e:
        # print(e)
        print("Come again?")
        # speak("come again?")
        return None
    return query

def wake():
    with sr.Microphone() as source:
        r = sr.Recognizer()
        audio = r.listen(source)
    query = r.recognize_google(audio, key=None, language="en-US", show_all=False)
    if wake_phrase in query:
        take_command()
    wish_me()

def call_ava():
    responding = False
    with sr.Microphone() as source:
        r = sr.Recognizer()
        audio = r.listen(source)
    query = r.recognize_google(audio, key=None, language="en-US", show_all=False)
    if call_phrase in query:
        responding = True
        take_command()
    else:
        responding = False
    check_user()

def check_user():
    if responding == False:
        speak("will that be all, sir?")
        with sr.Microphone() as source:
            r = sr.Recognizer()
            audio = r.listen(source)
        query = r.recognize_google(audio, key=None, language="en-US", show_all=False)
        if "yes" in query:
            speak(goodbye_phrase)
            sys.exit()
        if "no" in query:
            responding = True
            speak("what else can i do for you")
            take_command()

while listening:  
    wake()
    if __name__ == "__main__":
        while responding:
            query = take_command().lower()

            if "wikipedia" in query:
                speak("searching wikipedia")
                query = query.replace("wikipedia", "")
                query = query.replace("search", "")
                query = query.replace("in", "")
                results = wikipedia.summary(query, sentences = 2)
                speak("according to wikipedia")
                print(results)
                speak(results)
                call_ava()

            elif "define" in query:
                speak("searching wolfram alpha for definitions")
                query = query.replace("define", "")
                query = query.replace("hey", "")
                query = query.replace("hello", "")
                query = query.replace("ava", "")
                app_id = "QHA83L-568TKTG4TV"
                client = wolframalpha.Client(app_id)
                results = client.query(query, sentences = 2)
                answer = next(results.results).text 
                answer = answer.replace("1", ",")
                answer = answer.replace("2", ",")
                answer = answer.replace("3", ",")
                answer = answer.replace("4", ",")
                answer = answer.replace("5", ",")
                answer = answer.replace("6", ",")
                answer = answer.replace("7", ",")
                answer = answer.replace("8", ",")
                answer = answer.replace("9", ",")
                answer = answer.replace("10", ",")
                answer = answer.replace("noun", "")
                answer = answer.replace("verb", "")
                answer = answer.replace("adjective", "")
                answer = answer.replace("determiner", "")
                speak(answer)
                call_ava()

            elif "time" in query:
                hour = datetime.datetime.now().strftime("%H")
                minute = datetime.datetime.now().strftime("%M")

                if int(hour) > 12:
                    hour = int(hour)
                    hour -= 12

                time = (f"{hour}:{minute}")
                speak(f"the time is {time} sir")
                call_ava()

            elif "weather" in query:
                owm = pyowm.OWM("65f3e6f2b1532bd1b5165346615128c1")
                city = "Austin"
                austin = owm.weather_at_place("Austin, US")
                weather = austin.get_weather()
                austin_rain_check = owm.three_hours_forecast("Austin, US")
                temperature = weather.get_temperature("fahrenheit")["temp"]
                rain_check = austin_rain_check.will_have_rain()

                if rain_check == True:
                    speak(f"Currently, it is {temperature} degrees in austin, and no rain is expected")
                else:
                    speak(f"Currently, it is {temperature} degrees in austin and rain is expected")

                call_ava()

            elif "joke" in query:
                jokes_library.joking_around()
                call_ava()

            elif "goodbye" or "thank you for your time" or "done" in query:
                speak(goodbye_phrase)
                sys.exit()

错误信息将我引到第98行,在那里我有一个if语句说 if responding == False:我可能犯了一个愚蠢的错误,但谁能给我指出正确的方向?

python variables
1个回答
0
投票

试着在你的代码开始处添加一个全局变量语句,就在第一个 responding = True例如

global responding
responding = True
© www.soinside.com 2019 - 2024. All rights reserved.