import scipy as sy
import numpy as np
import re
from numpy import *
P = np.array(input('Please enter the P vector:\n'))
Q = np.array(input('\nPlease enter the Q vector:\n'))
R = np.array(input('\nPlease enter the R vector:\n'))
print('\n\nP: ',P)
print('Q: ',Q)
print('R: ',R)
#calculations for question 1
PQ=Q-P
magPQ= sy.sqrt(dot(PQ,PQ))
#calculations for question 2
PR= R-P
#calculations for question 3
QP = P-Q
QR = R-Q
magQP=sy.sqrt(dot(QP,QP))
magQR=sy.sqrt(dot(QR,QR))
angle=sy.arccos(dot(QP,QR)/(magQP*magQR))
angled=angle*180/sy.pi
#calculations for question 4
Area = sy.sqrt(dot(cross(PQ,QR),cross(PQ,QR)))
#calculations for question 5
magPR =sy.sqrt(dot(PR,PR))
perimeter = magPQ+magQR+magPR
######################OUTPUT#######################
print("Question 1. What is the distance between P and Q?")
print("Answer: ",round(q1,4))
print("\nQuestion 2. What is the distance vector from P to R?")
print('Answer: ',q2)
print("\nQuestion 3. What is the angle between QP and QR?")
print('Answer: ',angled)
print("\nQuestion 4. What is the area of the triangle PQR?")
print('Answer: ',Area)
print("\nQuestion 5. What is the perimeter of triangle PQR?")
print('Answer: ',perimeter)
上面是我的代码,当我尝试编译时,我收到错误
Traceback (most recent call last):
File "Homework1-3.py", line 17, in <module>
PQ=Q-P
TypeError: ufunc 'subtract' did not contain a loop with signature matching types dtype('<U6') dtype('<U6') dtype('<U6')
我能够输入我的数组非常重要,因为我希望这个程序能够解决问题。它似乎是使用字符串而不是浮点数。我怎么能解决这个问题?
答案 0 :(得分:2)
在互动的ipython(3)会话中,我尝试了:
shape
我强烈建议您启动一个交互式Python会话,并测试代码的每一步。让它一块一块地工作。编写一个在开头就有错误的完整脚本并不是一种有效的编程方式 - 或者是学习方法。
使用numpy数组时,请查看dtype
和cellAttrsDictionary
;不要认为数组是有意义的或有用的。检查一下。