简体   繁体   English

如何修复 python 中的“TypeError:'NoneType' 类型的参数不可迭代”

[英]How to fix 'TypeError: argument of type 'NoneType' is not iterable ' in python

I am building the VUI for a virtual assistant as a hobby project using pyttsx3 and SpeechRecognition.我正在使用 pyttsx3 和 SpeechRecognition 为虚拟助手构建 VUI 作为爱好项目。 i first made a function called take_order() then in the next function run_agos() i checked if take_Order() contains some keyword to start an other process, see this code:我首先制作了一个名为take_order()的 function 然后在下一个 function run_agos()我检查了take_Order()是否包含一些关键字来启动另一个进程,请参见以下代码:

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

listener = sr.Recognizer()
engine = pyttsx3.init()

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


def take_order():
    try:
        with sr.Microphone() as source:
            print('listening...')
            voice = listener.listen(source)
            command = listener.recognize_google(voice)
            command = command.lower()
            if 'alex' in command:
                command = command.replace('alexa', '')
                print(command)
            else:
                return
            return command
    except pass



def run_agos():
    task = take_order()

    if 'play' in task:
        song = task.replace('play', '')
        talk('playing ' + song)
        pywhatkit.playonyt(song)
    elif 'time' in task:
        time = datetime.datetime.now().strftime('%I:%M %p')
        print(time)
        talk('Current time is ' + time)
    elif 'who is' in task:
        person = task.replace('who is', '')
        try:
            info = wikipedia.summary(person, 1)
            print(info)
            talk(info)
        except wikipedia.exceptions.DisambiguationError as e:
            print(e.options)
            talk(person + 'may refer to')
            talk(e.options)
    elif 'what is' in task:
        thing = task.replace('what is', '')
        try:
            info = wikipedia.summary(thing, 1)
            print(info)
            talk(info)
        except wikipedia.exceptions.DisambiguationError as e:
            print(e.options)
            talk(thing + 'may refer to')
            talk(e.options)
    elif 'joke' in task:
        joke = pyjokes.get_joke()
        talk(joke)
        print(joke)
    else:
        talk('I did not hear that, pleas repeat the command.')
    return run_agos()


while True:
    run_agos()

At first it works fine, however after a while I get TypeError: argument of type 'NoneType' is not iterable and the program shuts down.起初它工作正常,但过了一会儿我得到TypeError: argument of type 'NoneType' is not iterable并且程序关闭。 I also get the extit code: -1073741819 (0xC0000005).How can I fix this error so it will keep running the program?我还得到了退出代码:-1073741819 (0xC0000005)。如何修复此错误,使其继续运行程序?

It looks like in the take_order function, you are either returning nothing when an error occurs or returning nothing as well when 'alexa' isn't in the command.看起来在 take_order function 中,您要么在发生错误时不返回任何内容,要么在命令中没有“alexa”时也不返回任何内容。 Then your code checks if 'play' in task .然后您的代码检查if 'play' in task Here, task is None since nothing was returned.在这里,任务是无,因为没有返回任何内容。 This is your main issue.这是你的主要问题。 I would suggest returning a specific string or a value for example -1.我建议返回一个特定的字符串或一个值,例如 -1。 Then, right after task = take_order() , check if task is that value.然后,在task = take_order()之后,检查 task 是否是那个值。 for example例如

def take_order():
    try:
        with sr.Microphone() as source:
            print('listening...')
            voice = listener.listen(source)
            command = listener.recognize_google(voice)
            command = command.lower()
            if 'alex' in command:
                command = command.replace('alexa', '')
                print(command)
            else:
                return -1
            return command
    except: return -1

def run_agos():
    task = take_order()
    if task == -1: return
    ...

One more thing: You always do return run_agos() at the end of run_agos() which recursively calls itself.还有一件事:你总是在 run_agos() 的末尾return run_agos() ,它递归地调用自己。 This is bad practice since you already have a while True loop that calls the function forever.这是不好的做法,因为您已经有一个while True循环永远调用 function。 I would suggest removing the return statement in the end of run_agos我建议删除run_agos末尾的 return 语句

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:“NoneType”类型的参数不可迭代需要修复 - TypeError: argument of type 'NoneType' is not iterable need a fix Python - TypeError:“NoneType”类型的参数不可迭代 - Python - TypeError: argument of type 'NoneType' is not iterable Python游戏| TypeError:“ NoneType”类型的参数不可迭代 - Python Game | TypeError: argument of type 'NoneType' is not iterable Python TypeError:“ NoneType”类型的参数不可迭代 - Python TypeError: argument of type 'NoneType' is not iterable TypeError:“ NoneType”类型的参数不可迭代python - TypeError: argument of type 'NoneType' is not iterable python Python聊天机器人“ TypeError:'NoneType'类型的参数不可迭代” - Python chatbot “TypeError: argument of type 'NoneType' is not iterable” python错误TypeError:'NoneType'类型的参数不可迭代 - The python error TypeError: argument of type 'NoneType' is not iterable TypeError:“NoneType”类型的参数在 python 中不可迭代 - TypeError: argument of type 'NoneType' is not iterable in python TypeError:NoneType 类型的参数在 python 中不可迭代 - TypeError: argument of type NoneType is not iterable in python 如何解决“'NoneType'类型的参数不可迭代”的问题? - How to fix this the problem of "argument of type 'NoneType' is not iterable"?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM