Python The inner product is <function innerproduct at 0x000001EF5A6A7D30> error

114 views Asked by At

My code is below, but I'm getting this weird output for my inner product code and don't know why it's not calculating the correct inner product. nums and nums2 ask the user for an equal list of numbers in which the inner product will be calculated. Any assistance would be appreciated.

def innerproduct(nums, nums2):
    sum = 0.0
    for i in range(len(nums)):
        sum += nums[i] * nums2[i]
    return innerproduct
2

There are 2 answers

4
Ashok Arora On BEST ANSWER

The error arising because of the return innerproduct statement since that is the name of the function.

Instead, did you mean to return the sum?

def innerproduct(nums, nums2):
    sum = 0.0
    for i in range(len(nums)):
        sum += nums[i] * nums2[i]
    return sum
0
Prune On

That's not "weird output", it's exactly what you told it to return. You ignored the result and returned a reference to the function object.

Try this instead:

result = 0
for i in range(len(nums)):
    result += nums[i] * nums2[i]
return result

Note: do not give a variable the same name as a built-in type or function.

You can do this more directly with the built-in sum function:

return sum(nums[i] * nums2[i] for i in range(len(nums)))

Or perhaps even better:

return sum(a * b for a, b in zip(nums, nums2[i]))