TypeError: 'function' object is not subscriptable - length issue

Viewed 81

I want to know what is bigger? Sum of numbers which index gives 1 or the sum of first half of the list. And I got this error.

TypeError: 'function' object is not subscriptable


def funky2(b):
    c = checker[:len(b)/2]
    a = sum(b) / len(b)
    if a > sum(c):
        return f'Statistic means is more than sum of 1 to half of list'
    else:
        return f'Statistic means is less than sum of 1 to half of list'



def checker(list):
    b = []
    for elem in list:
        if list.index(elem) % 3 == 1:
            b.append(elem)
    return funky2(b)
print(checker([1,2,3,4,5,6,7,8,9]))
1 Answers

checker[:len(b)/2] is causing the problem because checker is a function. Instead, you want to look at the first half of the list you need to use b. Additionally, you should be using // for integer division since normal division will return a float; and you cannot slice a list by a float. Another point is to try and avoid variable names that are the same as a type(here list)

def funky2(b):
    c = b[:len(b)//2]
    a = sum(b) / len(b)
    if a > sum(c):
        return f'Statistic means is more than sum of 1 to half of list'
    else:
        return f'Statistic means is less than sum of 1 to half of list'



def checker(lst):
    b = []
    for elem in lst:
        if lst.index(elem) % 3 == 1:
            b.append(elem)
    return funky2(b)
print(checker([1,2,3,4,5,6,7,8,9]))
Related