# File: peri.py # Compute the perimeter of a triangle; user enters the points defining it # WARNING: input is not error checked (see peri-c.py for how to do it right) # # Matt Bishop, ECS 10, Fall 2012 # import math # load up the math functions # # compute the distance between two points (ax, ay) and (bx, by) # def dist(ax, ay, bx, by): temp = (ax - bx) ** 2 + (ay - by) ** 2 return math.sqrt(temp) # # compute the perimeter of the triangle defined by (ax, ay), (bx, by), (cx, cy) # def compperi(ax, ay, bx, by, cx, cy): d1 = dist(ax, ay, bx, by) d2 = dist(ax, ay, cx, cy) d3 = dist(bx, by, cx, cy) return d1 + d2 + d3 # # construct the prompts # def prompt(xory, which): s = "Please enter the " + xory + " coordinate of the " + which + " point: " return s # ################ MAIN PROGRAM # # # read in the co-ordinates # try: iax = float(input(prompt("x", "first"))) iay = float(input(prompt("y", "first"))) ibx = float(input(prompt("x", "second"))) iby = float(input(prompt("y", "second"))) icx = float(input(prompt("x", "third"))) icy = float(input(prompt("y", "third"))) except: print("All co-ordinates must be real numbers!") else: # # print the corresponding triangle's perimeter # temp = compperi(iax, iay, ibx, iby, icx, icy) print("The perimeter is", temp)