Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 3 - Mathematical Functions, Strings, and Objects - Programming Exercises - Page 89: 3.17

Answer

import turtle x1, y1, x2, y2, x3, y3 = eval(input("Enter three points for a triangle: ")) side1 = ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5 side2 = ((x1 - x3) ** 2 + (y1 - y3) ** 2) ** 0.5 side3 = ((x2 - x3) ** 2 + (y2 - y3) ** 2) ** 0.5 s = (side1 + side2 + side3) / 2 area = (s * (s - side1) * (s - side2) * (s - side3)) ** 0.5 turtle.penup() turtle.goto(x1, y1) turtle.pendown() s = "p1 (" + str(x1) + ":" + str(y1) + ")" turtle.write(s) turtle.goto(x2, y2) s = "p2 (" + str(x2) + ":" + str(y2) + ")" turtle.write(s) turtle.goto(x3, y3) s = "p3 (" + str(x3) + ":" + str(y3) + ")" turtle.write(s) turtle.goto(x1, y1) turtle.penup() turtle.goto(x2, y2 - 20) turtle.write("The area is " + str(area)) turtle.done()

Work Step by Step

# (Turtle: triangle area) Write a program that prompts the user to enter the three # points p1, p2, and p3 for a triangle and display its area below the triangle, as # shown in Figure 3.7a. The formula for computing the area of a triangle is given # in Exercise 2.14. import turtle x1, y1, x2, y2, x3, y3 = eval(input("Enter three points for a triangle: ")) side1 = ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5 side2 = ((x1 - x3) ** 2 + (y1 - y3) ** 2) ** 0.5 side3 = ((x2 - x3) ** 2 + (y2 - y3) ** 2) ** 0.5 s = (side1 + side2 + side3) / 2 area = (s * (s - side1) * (s - side2) * (s - side3)) ** 0.5 turtle.penup() turtle.goto(x1, y1) turtle.pendown() s = "p1 (" + str(x1) + ":" + str(y1) + ")" turtle.write(s) turtle.goto(x2, y2) s = "p2 (" + str(x2) + ":" + str(y2) + ")" turtle.write(s) turtle.goto(x3, y3) s = "p3 (" + str(x3) + ":" + str(y3) + ")" turtle.write(s) turtle.goto(x1, y1) turtle.penup() turtle.goto(x2, y2 - 20) turtle.write("The area is " + str(area)) turtle.done()
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.