【发布时间】:2016-06-02 19:44:08
【问题描述】:
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')
能够输入我的数组非常重要,因为我希望这个程序能够解决问题。它似乎使用字符串而不是浮点数。我该如何解决这个问题?
【问题讨论】:
-
np.array(input('Please enter the P vector:\n'))- 您是否希望 解析 用户键入的字符串?np.array不会那样做。 -
不知何故我的 python 更新到 3.x,所以它似乎在 python 2.x 中有效
-
如果您在 Python 2 上使用此代码,
input会在字符串上调用eval(这是一个糟糕的想法,在 Python 3 中进行了正确的更改)。您还遇到了其他问题,例如使用print错误。 -
我修复了 python 3 的打印,因为它是第一组抛出的错误消息。
-
我删除了 np.从数组中,我仍然收到相同的错误消息
标签: python-3.x numpy scipy