I am trying to write a function that takes each element within two strings of equal length and multiply them by each other and then add each product to get a single answer. vector1 = [1, 2, 3] vector2 = [4, 5, 6]
For example the above would be (1*4) + (2*5) + (3 * 6) = 32
So far this is what I have:
vector1 = [1, 2, 3]
vector2 = [4, 5, 6]
ans = 0
if len(vector1) == 0 or len(vector2) == 0:
print("Invalid vectors")
elif len(vector1) != len(vector2):
print("Invalid vectors")
else:
for i in range (len(vector1)):
temp = vector1(i) * vector2(i)
ans = ans + temp
The code gives me the following error when compiling:
TypeError: 'list' object is not callable
I tried changing the above code to be more like
vector1 = [1, 2, 3]
vector2 = [4, 5, 6]
ans = 0
if len(vector1) == 0 or len(vector2) == 0:
print("Invalid vectors")
elif len(vector1) != len(vector2):
print("Invalid vectors")
else:
for i in range (len(vector1)) and i in range(len(vector2)):
temp = vector1(i) * vector2(i)
ans = ans + temp
But that doesn't work either. What am I doing wrong here?
Edit: After running the code through Python Visualizer, the following line is giving me the specific problem here:
temp = vector1(i) * vector2(i)