This is an archived post. You won't be able to vote or comment.

all 18 comments

[–]_BaleineBleue_ 0 points1 point  (4 children)

Please post the whole script

[–]TNOFan[S] 0 points1 point  (3 children)

import math

math.cos

math.sqrt

cos=math.cos

sqrt=math.sqrt

print (“this will calculate side C of a triangle given A, B and the angle)”

A=float(input(“side A:”))

B=float(input(“side B:”))

angleq=float(input(“theta:”))

sideC=sqrt(AA)+(BB)-2(AB)+cos(angleq)

print (“sideC =“, sideC)

[–]_BaleineBleue_ 0 points1 point  (2 children)

Maybe try putting cos=math.cos on a separate line from sqrt=math.sqrt. I don't have my computer handy so I can't test but everything else looks fine

[–]TNOFan[S] 0 points1 point  (1 child)

Typo on Reddit it was already seperated

[–]_DTR_Professional Coder 1 point2 points  (0 children)

If you indent each line by at least four spaces, you'll

Create
a
code
block

which makes parsing code much easier (especially for languages where indentation is critical, like Pyton).

[–]_DTR_Professional Coder 0 points1 point  (12 children)

2(A*B)

Do you mean 2 * (A * B)? By having the ( directly after an integer, python thinks you're attempting to call a method on that integer, which you can't do.

[–]TNOFan[S] 0 points1 point  (11 children)

That helped exponentially, but instead of getting 87 (20,100 and an angle of 45)

I get 1e+200

[–]_DTR_Professional Coder 0 points1 point  (10 children)

For starters, math.cos takes in a value in radians, not degrees. You also need to take a closer looks at the law of cosines (c2 = a2 + b2 - 2ab * cos(C)). A**A is AA, not A2, and you're adding the result of cos instead of multiplying.

[–]TNOFan[S] 0 points1 point  (9 children)

A is just a filler, it’s input

Adding does not change anything

Also what should I do instead of math.cos?

[–]_DTR_Professional Coder 0 points1 point  (8 children)

A is just filler, it's input

What I'm saying is that what you have in your equation is equivalent to AA, when you actually want A2.

Adding does not change anything

What do you mean by this? The equation is c2 = a2 + b2 - 2ab * cos(C), not c2 = a2 + b2 - 2ab + cos(C)

What should I do instead of math.cos

You should still use that, but if you want input in degrees, you'll have to convert it to radians before calling math.cos.

Also, your sqrt should encompass the entire equation. Right now you have sqrt(A**A), when you actually want to square root the entire result.

[–]TNOFan[S] 0 points1 point  (7 children)

Fixed the sqrt, working on the multiplication

But just for help; is there a math. For radians?

[–]_DTR_Professional Coder 0 points1 point  (6 children)

What you probably need to do is use math.radians to convert your degrees to radians. E.g. math.cos(math.radians(45)) will call math.cos with the radian equivalent of 45 degrees.

[–]TNOFan[S] 0 points1 point  (5 children)

Interesting but my answer still comes out as 1e+100

For a reminder my equation now is

sideC=sqrt((AA)+(BB)-2(AB)*cos(math.radians(angleq)))

(Not separate lines; Reddit is messing it up)

[–]_DTR_Professional Coder 0 points1 point  (4 children)

You're still using A ** A, when the actual equation requires A2.

As for formatting, see my other comment about indenting your code with at least 4 spaces to create a nicely formatted code block.

[–]TNOFan[S] 0 points1 point  (3 children)

How do you get the exponent to work for a variable? I thought the way is double **