PythonFun PythonFun - 26 days ago 12
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):]
print(max_dot_product(a,b,n))


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

Answer

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

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