PythonFun PythonFun - 11 months ago 88
Python Question

Python: TypeError: 'NoneType' object is not subscriptable

#Uses python3

import sys

def max_dot_product(a,b,n):

a = a.sort(reverse=True)
b = b.sort(reverse=True)
res = 0

for i in range(n):
res += a[i]*b[i]
return res

if __name__ == '__main__':
data = [int(x) for x in input("").split()]
n = data[0]
a = data[1:(n + 1)]
b = data[(n + 1):]

When I run this code, it reveals that "TypeError: 'NoneType' object is not subscriptable".

Answer Source

sort sorts a list in place and (implicitly) returns None. Drop the assignment of the return value and you should be OK: