TypeError: 'NoneType' object is not iterable in Python

Dung Do Tien Apr 27 2022 152

Hi all guys. I'm a student and my school has a subject, that teaches learn about Python. I have created some lines of code to help find some employees from a list of data. Like this:

 def find_employee(depar_names):
    new_depar_names = []
    for s in depar_names:
        if s.startswith("B"):
            new_depar_names.append(s)
            
def display_employee(depar_names):
    for a in depar_names:
        print(a)
        
employess = ["Bob", "Marry", "June", "Sammy"]
good_employee_name = find_employee(employess)

display_employee(good_employee_name)    

But When I was running the code above and got an exception TypeError: 'NoneType' object is not iterable. 

 Traceback (most recent call last):
  File "main.py", line 16, in <module>
    display_employee(good_employee_name)
  File "main.py", line 10, in display_employee
    for a in depar_names:
TypeError: 'NoneType' object is not iterable

Anyone can help me explain it?

I'm using Python 3.9 and Windows 11.

Thanks in Advance.

Have 1 answer(s) found.
  • C

    Carlos Yánez Apr 27 2022

    You got a mistake that's very basic. you forgot return value for your find_employee() function.  When you can this line:

     good_employee_name = find_employee(employess)

    Some value returns from find_employee() will be assigned to good_employee_name variable.

    Below is the full code for you:

     def find_employee(depar_names):
        new_depar_names = []
        for s in depar_names:
            if s.startswith("B"):
                new_depar_names.append(s)
                return new_depar_names
                
    def display_employee(depar_names):
        for a in depar_names:
            print(a)
            
    employess = ["Bob", "Marry", "June", "Sammy"]
    good_employee_name = find_employee(employess)
    
    display_employee(good_employee_name)

    And the output is Bob.

    I hope this answer is all you need.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close