How to Multiply the numbers within a string with another string of equal length and then add each product in Python
<p>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]</p>

<p>For example the above would be (1*4) + (2*5) + (3 * 6) = 32</p>

<p>So far this is what I have:</p>

<pre><code>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
</code></pre>

<p>The code gives me the following error when compiling:</p>

<p>TypeError: 'list' object is not callable</p>

<p>I tried changing the above code to be more like</p>

<pre><code>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
</code></pre>

<p>But that doesn't work either. What am I doing wrong here?</p>

<p>Edit: After running the code through Python Visualizer, the following line is giving me the specific problem here: </p>

<p>temp = vector1(i) * vector2(i)</p>

<p>The corrected version of your code.</p>

<pre><code>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 += temp
print ans
</code></pre>

<p>All you have to do is change <code>vector1(i)</code> to <code>vector[i]</code>, to access the element at index <code>i</code> in the lists</p>

python python-3.x