首页 > 解决方案 > python : Why this code did not work? (invalid syntax)

问题描述

enter image description here

hi im studying python and i dont know why this code did not work.

can you help me ? thanks

标签: pythonnumpy

解决方案


There are three errors. First, print is a function in Python 3, not a statement, so you need to use parentheses. The second is that with A*B you are trying to multiply regular Python lists, but you cannot do that. Also, A+B works, but it concatenates the lists instead of adding their elements, which is probably not what you wanted. You should use the definitions

A = np.array([2, 1, -1])
B = np.array([1, -1, 2])

The third is that you are trying to multiply matrices of incompatible dimensions in the third section.

By the way, the np.matrix class is deprecated, so you should not use it.


推荐阅读