'TypeError: argument of type 'NoneType' is not iterable in Jarvis A.I

I am very sorry for asking a noob question because I am a very new guy to python.

My program is an A.I. like Jarvis.

My problem is that when I run my program it gives me an error:

TypeError: argument of type 'NoneType' is not iterable

The full error is:

**C:\Users\jobro\anaconda3\envs\Jarvis\pythonw.exe C:/Users/jobro/PycharmProjects/Jarvis/main.py
Please Speak...

    Voice Input :  None
    Traceback (most recent call last):
      File "C:/Users/jobro/PycharmProjects/Jarvis/main.py", line 61, in <module>
        if validate_input(voice_input, greeting_cmd):
      File "C:/Users/jobro/PycharmProjects/Jarvis/main.py", line 21, in validate_input
        if cmd in input:
    TypeError: argument of type 'NoneType' is not iterable
    
    Process finished with exit code 1**

Here is my code:

import pyttsx3
import os
import platform
import speech_recognition as sr

speech = sr.Recognizer()

# Getting OS name
os_name = platform.platform()

# Commands
launch_cmd = ['open', 'launch', 'run']
greeting_cmd = ['hello jarvis', 'hi jarvis', 'wakeup jarvis']

launch_dict = {'project': 'C:\\Users\\jobro\\PycharmProjects', 'projects': 
               'C:\\Users\\jobro\\PycharmProjects',
               'youtube': 'https://www.youtube.com/'}


def validate_input(input, commands):
    for cmd in commands:
        if cmd in input:
            return True


def get_launch_value(voice_input):
    for key, value in launch_dict.items():
        if key in voice_input:
            return value


try:
    engine = pyttsx3.init()
except Exception as e:
    print(e)

voices = engine.getProperty('voices')

engine.setProperty('voice', 
'HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Speech\\Voices\\Tokens\\TTS_MS_EN-US_DAVID_11.0')
rate = engine.getProperty('rate')
engine.setProperty('rate', 150)


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


def read_voice_cmd():
    print('Please Speak...')
    with sr.Microphone() as source:
        audio = speech.listen(source, timeout=5, phrase_time_limit=5)
    try:
        return speech.recognize_google(audio_data=audio).lower()
    except Exception as e:
        print(e)


while True:
    voice_input = read_voice_cmd()
    print('Voice Input : ', voice_input)
    if validate_input(voice_input, greeting_cmd):
        speak('Hello Sir, How may I help you?')
        continue
    elif validate_input(voice_input, launch_cmd):
        path = get_launch_value(voice_input)
        if os_name == 'Windows-10-10.0.19041-SP0':
            cmd = 'start '
        else:
            cmd = 'explorer '
        if os_name == 'Darwin':
            cmd = 'start'
        cmd = cmd + path

        os.system(cmd)

    else:
        speak('Command not found.')
        continue

Please help me!



Solution 1:[1]

In your main loop you start with

while True:
    voice_input = read_voice_cmd()
    print('Voice Input : ', voice_input)

This is printing

Voice Input :  None

So you're not detecting any voice input. Then you pass this value of None to the validate_input function, and try to check if it contains certain commands using if cmd in input:

Since a in b requires b to be a sequence, and None is not an iterable, this throws an error. Concentrate on fixing the voice input part, so that you actually detect some speech, and the print statement does not return Voice input : None.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pines