如何在Python中循环函数?

2024-05-15 01:44:45 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我最近一直在学习python,我发现了一个关于如何制作人工助手的教程。我有一些关于python的基础/中级知识,但我无法找出我遇到的错误。它们出现在我添加while循环之后。我也是这个平台的新手,所以如果我犯了任何错误,请原谅我,谢谢你

代码如下:

import speech_recognition as sr
import pyttsx3
import pywhatkit
import datetime
import wikipedia

listener = sr.Recognizer()
engine = pyttsx3.init()
voices = engine.getProperty('voices')
rate = engine.getProperty('rate')
engine.setProperty('rate', rate-35)
engine.setProperty('voice', voices[2].id)


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


def take_command():
    try:
        with sr.Microphone() as source:
            print('I\'m Listening')
            voice = listener.listen(source)

            command = listener.recognize_google(voice)
            command = command.lower()
            if 'jarvis' in command:
                command = command.replace('jarvis', '')
                print('I heard ' + '\"'+command+' \"')
    except:
        pass
    return command


def run_jarvis():
    command = take_command()
    if 'play' in command:
        song = command.replace('play', '')
        speak('Playing' + song)
        pywhatkit.playonyt(song)
        print('Playing' + song)

    elif "what time is it" in command:
        time = datetime.datetime.now().strftime('%H:%M')
        speak('It is' + time)
        print(time)

    elif 'who is' in command:
        person = command.replace('who is', '')
        info = wikipedia.summary(person, 1)
        print(info)
        pywhatkit.search(person)
        speak(info)

    elif 'search' in command:
        question = command.replace('search', '')
        pywhatkit.search(question)

    else:
        speak('Sorry, I couldn\'t understand.')
        speak('Can you repeat?')
        print('Sorry, I couldn\'t understand.')


while True:
    run_jarvis()

下面是我得到的错误:

回溯(最近一次呼叫最后一次):

文件“C:/Users/CAN/PycharmProjects/Jarvis 0.1/Jarvis.py”,第67行,在 run_jarvis()

文件“C:/Users/CAN/PycharmProjects/Jarvis 0.1/Jarvis.py”,第37行,在run_Jarvis中 command=take_command()

take_命令第33行的文件“C:/Users/CAN/PycharmProjects/Jarvis 0.1/Jarvis.py” 返回命令

UnboundLocalError:赋值前引用的局部变量“command”

进程已完成,退出代码为1


Tags: runinimportratetimesongenginecommand
3条回答

在我所知道的每一种现代编程语言中,必须先声明变量,然后才能使用它。例如,在JavaScript中,必须写入var x;,然后才能使用变量名x。但是Python不知道声明的命令(您不能只写x),但必须分配它。如果您不知道该值,只想告诉解释器该变量存在,那么可以编写x = None

我们制作了一个机器人,在github上发现了很多关于同一件事的问题。 问题是,command返回为None,假设您的API返回了对我们不可用的内容。然后它返回这个。 要使这个机器人完美工作,请确保您有稳定的互联网连接和良好的pc。 要防止程序停止,请在执行命令之前添加此行

command = ''

这不会解决问题,但肯定不会让你退出计划

这里似乎有错误

def take_command():
    try:
        with sr.Microphone() as source:
            print('I\'m Listening')
            voice = listener.listen(source)

            command = listener.recognize_google(voice)
            command = command.lower()
            if 'jarvis' in command:
                command = command.replace('jarvis', '')
                print('I heard ' + '\"'+command+' \"')
    except:
        pass
    return command

发生这种情况是因为未定义command。在定义command之前,您的try部分发生错误。因此,它会排除并尝试return command。尽管像我上面说的,命令并没有定义。因此,如果您except:pass而不是pass,则将其设置为return None

更新:评论中有人建议将return command放在try块中,这样更有意义

def take_command():
    try:
        with sr.Microphone() as source:
            print('I\'m Listening')
            voice = listener.listen(source)

            command = listener.recognize_google(voice)
            command = command.lower()
            if 'jarvis' in command:
                command = command.replace('jarvis', '')
                print('I heard ' + '\"'+command+' \"')
            return command
    except:
        return None
    

如何检查引发的错误

    except Exception as e:
        print(e)

相关问题 更多 >

    热门问题