Код выдаёт "TypeError: 'NoneType' object is not iterable" как исправить?

Написал код для сравнения на вхождение элементов списка в словарь и наоборот.

def Function():
    print('Для остановки ввода словаря в ключе напечатайте пробел')
    dic={}
    while True:
        x1=input('Введите ключ: ')
        if x1 != ' ':
          x2 = input('Введите значение: ')
          dic[x1]=x2
        else:
            break
    print(dic)
    tex=[]
    tex = input('Введите список: ')
    text1 = []
    text2 = []
    for i in tex:
        for key in dic.keys():
            if (i in key) and str(i) not in text1:
                text1.append(i)
    for i in tex:
        for value in dic.values():
            if (i in value) and str(i) not in text1:
                text1.append(i)
    for i in tex:
        for value in dic.values():
            if (str(value) not in text1) and (str(value) not in text2)and (str(i) != ',') and (str(i) != ' '):
                text2.append(str(value))
    for i in tex:
        for key in dic.keys():
            if (str(key) not in text1) and (str(key) not in text2) and (str(i) != ',') and (str(i) != '  '):
                text2.append(str(key))
    for i in tex:
        if (str(i) not in text1) and (str(i) not in text2) and (str(i) != ','):
            text2.append(str(i))
    print('Имеющиеся элементы списка в словаре и наоборот: ', text1, '\nНе имеющиеся элементы словаря в списке и наоборот: ', text2)
    

Function()

Позже решил переделать.

def x(x1=0,x2=0):
    print('Для остановки ввода словаря в ключе напечатайте пробел')
    dic={}
    while True:
        x1=input('Введите ключ: ')
        if x1 != ' ':
          x2 = input('Введите значение: ')
          dic[x1]=x2
        else:
            break
    print(dic)

def tex(tex=[]):
    tex = input('Введите список: ')
    


def Function(dic, tex):
    text1 = []
    text2 = []
    for i in tex:
        for key in dic.keys():
            if (i in key) and str(i) not in text1:
                text1.append(i)
    for i in tex:
        for value in dic.values():
            if (i in value) and str(i) not in text1:
                text1.append(i)
    for i in tex:
        for value in dic.values():
            if (str(value) not in text1) and (str(value) not in text2)and (str(i) != ',') and (str(i) != ' '):
                text2.append(str(value))
    for i in tex:
        for key in dic.keys():
            if (str(key) not in text1) and (str(key) not in text2) and (str(i) != ',') and (str(i) != '  '):
                text2.append(str(key))
    for i in tex:
        if (str(i) not in text1) and (str(i) not in text2) and (str(i) != ','):
            text2.append(str(i))
    print('Имеющиеся элементы списка в словаре и наоборот: ', text1, '\nНе имеющиеся элементы словаря в списке и наоборот: ', text2)

dic=x()
tex=tex()

Function(dic=dic, tex=tex)

После этого код начал выводить ошибку:

Traceback (most recent call last):
  File "G:\Python\Test.py", line 45, in <module>
    Function(dic=dic, tex=tex)
  File "G:\Python\Test.py", line 21, in Function
    for i in tex:
TypeError: 'NoneType' object is not iterable

Что не так?


Ответы (2 шт):

Автор решения: Roman Konoval

У вас в функции tex пропущен return. Такая функция всегда возвращает None. Поэтому дальше, когда используется результат работы tex, получаете исключение.

Исправить просто:

def tex(tex=[]):
    tex = input('Введите список: ')
    return tex
→ Ссылка
Автор решения: Bik1104

Исправленный код:

def x(x1=0,x2=0):
    print('Для остановки ввода словаря в ключе напечатайте пробел')
    dic={}
    while True:
        x1=input('Введите ключ: ')
        if x1 != ' ':
          x2 = input('Введите значение: ')
          dic[x1]=x2
        else:
            break
    print(dic)
    return dic
   
def tex(tex=[]):
    tex = input('Введите список: ')
    return tex


def Function(dic, tex):
    text1 = []
    text2 = []
    for i in tex:
        for key in dic.keys():
            if (i in key) and str(i) not in text1:
                text1.append(i)
    for i in tex:
        for value in dic.values():
            if (i in value) and str(i) not in text1:
                text1.append(i)
    for i in tex:
        for value in dic.values():
            if (str(value) not in text1) and (str(value) not in text2)and (str(i) != ',') and (str(i) != ' '):
                text2.append(str(value))
    for i in tex:
        for key in dic.keys():
            if (str(key) not in text1) and (str(key) not in text2) and (str(i) != ',') and (str(i) != '  '):
                text2.append(str(key))
    for i in tex:
        if (str(i) not in text1) and (str(i) not in text2) and (str(i) != ','):
            text2.append(str(i))
    print('Имеющиеся элементы списка в словаре и наоборот: ', text1, '\nНе имеющиеся элементы словаря в списке и наоборот: ', text2)

dic=x()
tex=tex()

Function(dic=dic, tex=tex)
→ Ссылка