I am getting a TypeError: list indices must be integers or slices, not float on the fourth line of code when I run this. nums and nums2 are simply asking the user to provide a list of numbers that will be used to calculate the inner product in the code that I have provided.
def innerproduct(nums, nums2):
for i in nums:
if i in nums2:
sum +- nums[i] * nums2[i]
return innerproduct
I'm not sure why this error is occurring and how to resolve this issue, so any guidance would be appreciated.
for i in nums
means thati
will store the value in nums list, not the index.for example if nums = [1.5, 6.2, 0.1] then i would be
1.5
andnums[i]
is throwing that error.either you can do
for i in range(len(nums)):
and getnums[i]
or justfor i in nums
and then sum +=i
instead ofnums[i]