0

How would I go about computing the area of a triangle in python? For example the base is 3 and the height is 8.

Coding guidance

27th May 2020, 4:11 AM
John Perez
John Perez - avatar
5 Answers
+ 5
If your struggle is with organizing the code, I'll take a stab at guiding you with approaching the thought process. First, let's get the formula down. Area of a parallelogram is: A = base * height or A = bh A triangle is basically half of a parallelogram. If you split the parallelogram diagonally from one corner to the opposite, the it becomes two identical triangles. Likewise, if you take any triangle and duplicate it, you can combine them to make a parallelogram. Therefore, the area of a triangle is: A = 1/2 * base * height or A = 1/2bh or A = bh/2 These are all the same. Next, I'll approach how to organize this into code...
27th May 2020, 5:48 AM
David Carroll
David Carroll - avatar
+ 5
Let's continue with using your example numbers. If base is 3 and height is 8, the following will work like a calculator: ---- print(3*8/2) ---- Outputs: 12.0 Let's add some variables: ---- base = 3 height = 8 print(base*height/2) ---- Output: 12.0 Let's move this into a function: ---- def triangle_area(base, height): return base * height / 2 print(triangle_area(3, 8)) ---- Output: 12.0 Hopefully, this helps.
27th May 2020, 5:53 AM
David Carroll
David Carroll - avatar
+ 4
Do you actually have trouble with the mathematical formula? Here is your clue. https://www.khanacademy.org/math/basic-geo/basic-geo-area-and-perimeter/area-triangle/a/area-of-triangle You have completed the SL Python course but haven't posted any codes yet. Have you actually tried any of the example codes? Just review this lesson and click on "Try it yourself" https://www.sololearn.com/learn/Python/2430/ What you are asking should be possible to do after 2 minutes of learning coding...
27th May 2020, 4:22 AM
Tibor Santa
Tibor Santa - avatar
+ 2
Yeah I was having trouble formulating the math portion I havent touched algebra in a long time so I was out of touch with the whole thing. I have a general idea for the variables and such it was just piecing it together in a formula sense was the issue and yeah I know I'm certified on here but I do all my coding on my laptop via text editor I only practice on here on the go when I can.
27th May 2020, 4:31 AM
John Perez
John Perez - avatar
+ 2
Yes it helps me immensely thank you guys for the help, I've never been good with math so when it comes to coding it it throws me off a bit.
27th May 2020, 11:45 AM
John Perez
John Perez - avatar