Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams 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)

Above is my code, when I try to compile, I get the error

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')

It's pretty important I am able to input my arrays because I would like this program to be able to solve problems. It seems like its using string instead of float. How could I fix this?

np.array(input('Please enter the P vector:\n')) - are you expecting that to parse the string the user types? np.array doesn't do that. – user2357112 Jun 2, 2016 at 19:50 If you were using this code on Python 2, input was calling eval on the string (which is a terrible idea and rightfully changed in Python 3). You also had other problems, like using print wrong. – user2357112 Jun 2, 2016 at 19:57 I fixed the print for python 3 because it was the first set of error messages thrown out. – Jacob Whitten Jun 2, 2016 at 20:00 In [375]: np.array(x) Out[375]: array('123,232,232', # trying to make that an array - still string dtype='<U11') In [377]: np.array(x.split(',')) Out[377]: array(['123', '232', '232'], # better - 3 strings dtype='<U3') In [378]: np.array(x.split(','),dtype=float) Out[378]: array([ 123., 232., 232.]) # good

I'd strongly suggest you fire up an interactive Python session, and test each step of the code. Get it working piece by piece. Writing a whole script that has bugs right at the start is not a productive way to program - or to learn.

And when using numpy arrays, look at the shape and dtype; do not assume that the array is something meaningful or useful. Check it.

Thank you, and wise words although when I try the last line of code I recieve Traceback (most recent call last):File "Homework1-3.py", line 8, in <module> P = np.array(P.split(','),dtype=float) AttributeError: 'numpy.ndarray' object has no attribute 'split' – Jacob Whitten Jun 2, 2016 at 20:14 You apply split to a string as produced by input, not to something that has been passed through np.array. – hpaulj Jun 2, 2016 at 21:28

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.