TransWikia.com

TypeError: object of type 'NoneType' has no len(). Codewars

Stack Overflow на русском Asked by Rollan on November 22, 2021

Код работает, но на CODEWARS Выводит ниже приписка и поэтому не получается cдать кату:

Traceback (most recent call last):
  File "main.py", line 21, in <module>
    test.assert_equals(comp(a1, a2), False)
  File "/home/codewarrior/solution.py", line 4, in comp
    if len(a) != len(b):
TypeError: object of type 'NoneType' has no len()
#Сравнивает два списка, второй должен состоять из квадратов первого, тогда True
def comp(a, b):
    if a and b is None:
        return True
    if len(a) != len(b):
        return False
    index = 0
    n = 0
    while index != len(a):
        for i in range(0, len(a)):
            if a[index]**2 == b[i]:
                n += 1
                continue
            else:
                continue
        if n != len(a):
            index += 1
    if n/2 == len(a):
        return True
    else:
        return False

One Answer

Попробуйте так:

def comp(a, b):
    if a is None and b is None:
        return True
    if a is None or b is None or len(a) != len(b):
        return False            
    return sorted(list(map(lambda x: x**2, a))) == sorted(b)

Тесты:

In [24]: comp([], [1,2])
Out[24]: False

In [25]: comp([], [])
Out[25]: True

In [26]: comp([4, 2, 3], [9, 16, 4])
Out[26]: True

Answered by MaxU on November 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP