Python Forum
TypeError: argument of type 'NoneType' is not iterable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: argument of type 'NoneType' is not iterable
#1
Question 
Every time i try to run the bellow code it gives me back this Error message
(TypeError: argument of type 'NoneType' is not iterable)
can anyone tell me where exactly the problem

import pyttsx3 
import speech_recognition as sr
import webbrowser
import time
import datetime
import os
import subprocess
import asyncio
from pydub import AudioSegment
from pydub.playback import play

#import pyautogul 

wel = pyttsx3.init()
voices = wel.getProperty('voices')
wel.setProperty('voice', voices[0].id)
def Speak(audio):
    wel.say(audio)
    wel.runAndWait()

def TakeCommands():
    command = sr.Recognizer()
    with sr.Microphone() as mic:
        print ('say commands sir ..')
        command.phrase_threshold = 0.4
        audio = command.listen(mic)
        try :
            print ('Recording ...')
            query = command.recognize_google(audio, language = 'en')
            print (f'you said : {query}')
        except Exception as Error:
            return None
        return query.lower()

music = AudioSegment.from_mp3('sounds/welcom.mp3')
play (music)


while True:
    query = TakeCommands()
    if 'open phootoshop' in query:
        b = AudioSegment.from_mp3('sounds/absher.mp3')
        play(b)
Reply
#2
(Nov-28-2024, 04:12 PM)Tajaldeen Wrote: it gives me back this Error message
(TypeError: argument of type 'NoneType' is not iterable)
Please post the entire error message. It is the only way to diagnose the problem.
Most of the time an error like 'Nonetype' is not iterable occurs because a function returned None to a code that was expecting another value.
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
(Nov-28-2024, 07:33 PM)Gribouillis Wrote:
(Nov-28-2024, 04:12 PM)Tajaldeen Wrote: it gives me back this Error message
(TypeError: argument of type 'NoneType' is not iterable)
Please post the entire error message. It is the only way to diagnose the problem.
Most of the time an error like 'Nonetype' is not iterable occurs because a function returned None to a code that was expecting another value.

This is the whole messages :

Error:
say commands sir .. Recording ... Traceback (most recent call last): File "c:\Users\pc\Desktop\AI\main.py", line 41, in <module> if 'open' in query: ^^^^^^^^^^^^^^^ TypeError: argument of type 'int' is not iterable PS C:\Users\pc\Desktop\AI>
buran write Nov-29-2024, 05:06 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#4
(Nov-29-2024, 03:37 AM)Tajaldeen Wrote: This is the whole messages :
This error message does not correspond to the code that you wrote above. Clearly the error messages occurs because the query variable returned by TakeCommands() is an integer, but the function that you wrote above cannot return an integer, unless you replaced the None value in the Exception handling part by an integer such as 0 for example.

As the message 'You said ...' does not appear in the message, it means that the call to recognize_google() failed and throwed an exception.

The code that calls TakeCommands() must handle the case when this function failed to get a valid command.
« We can solve any problem by introducing an extra level of indirection »
Reply
#5
(Nov-29-2024, 06:37 AM)Gribouillis Wrote:
(Nov-29-2024, 03:37 AM)Tajaldeen Wrote: This is the whole messages :
unless you replaced the None value in the Exception handling part by an integer such as 0 for example.

same error with 0 value
Reply
#6
(Nov-29-2024, 08:00 AM)Tajaldeen Wrote: same error with 0 value
Again I repeat that the code that calls TakeCommands() must take into account the fact that this function may occasionally return None or 0. Your code ignores this fact and this is why you have the error.
« We can solve any problem by introducing an extra level of indirection »
Reply
#7
The error you're encountering (TypeError: argument of type 'NoneType' is not iterable) is happening because the TakeCommands() function can return None when an exception occurs during the recognition process in the except block. Later in your while True loop, you're trying to check if 'open phootoshop' is in query, but if query is None, this results in the error, as None cannot be iterated over.

### The Cause:
In this line of code:
`python
if 'open phootoshop' in query:
`
You are assuming that query will always be a string. However, if the recognize_google() function fails and an exception is raised, your except block returns None. When None is passed into query, the check 'open phootoshop' in query tries to iterate over None, leading to the TypeError.

### Solution:
You need to check if query is not None before attempting to check if the string 'open phootoshop' is in it. This can be done by adding a simple conditional check.

Here’s how you can modify the code to fix the error:

### Modified Code:


import pyttsx3 
import speech_recognition as sr
import webbrowser
import time
import datetime
import os
import subprocess
import asyncio
from pydub import AudioSegment
from pydub.playback import play

wel = pyttsx3.init()
voices = wel.getProperty('voices')
wel.setProperty('voice', voices[0].id)

def Speak(audio):
    wel.say(audio)
    wel.runAndWait()

def TakeCommands():
    command = sr.Recognizer()
    with sr.Microphone() as mic:
        print('Say commands, sir..')
        command.phrase_threshold = 0.4
        audio = command.listen(mic)
        try:
            print('Recording...')
            query = command.recognize_google(audio, language='en')
            print(f'You said: {query}')
        except Exception as Error:
            return None
        return query.lower()

music = AudioSegment.from_mp3('sounds/welcom.mp3')
play(music)

while True:
    query = TakeCommands()
    if query:  # Check if the query is not None or empty
        if 'open phootoshop' in query:
            b = AudioSegment.from_mp3('sounds/absher.mp3')
            play(b)
### Key Changes:
1. **Check if query is valid**:
Before checking if 'open phootoshop' is in query, we check if query is not None using the condition if query:.
This ensures that the program only tries to process query if it contains a valid string.

### Explanation:
- The condition if query: ensures that query is neither None nor an empty string (""), so it will only attempt to perform the string check ('open phootoshop' in query) if query is a valid string.

This will prevent the TypeError from occurring and make your program more robust when the speech recognition fails to return a valid command.
buran write Nov-29-2024, 08:48 AM:
Spam link removed
Tajaldeen likes this post
Reply
#8
(Nov-29-2024, 08:47 AM)warmth1 Wrote: ### Solution:

You are genius , problem has scattered away .... thank you very much sir Heart Heart Heart
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Did you know that map() accepts more than one iterable? DeaD_EyE 2 330 Apr-13-2025, 10:16 AM
Last Post: noisefloor
  TypeError: Diagram.render() takes 1 positional argument but 2 were given sachin1361 0 1,212 Apr-23-2024, 06:39 AM
Last Post: sachin1361
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 3,516 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  TypeError: 'NoneType' object is not callable akbarza 4 13,718 Aug-24-2023, 05:14 PM
Last Post: snippsat
  i want to use type= as a function/method keyword argument Skaperen 9 3,580 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  TypeError: 'NoneType' object is not subscriptable syafiq14 3 6,753 Sep-19-2022, 02:43 PM
Last Post: Larz60+
  TypeError: unsupported operand type(s) for +: 'dict' and 'int' nick12341234 1 11,274 Jul-15-2022, 04:04 AM
Last Post: ndc85430
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 6,672 Jul-01-2022, 01:23 PM
Last Post: deanhystad
Question how to solve `'TypeError: 'int' object is not iterable`? netanelst 2 2,742 May-24-2022, 12:03 PM
Last Post: deanhystad
  TypeError: unsupported opperand type(s) for %: 'int' and 'list' cool_person 7 3,559 May-07-2022, 08:40 AM
Last Post: ibreeden

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020