想知道是否有人可以在最后一个字符串(面积,周长)中指出我的错误 - 任何帮助将不胜感激。)
message = Text(Point(5, 0.5),"The perimeter is: {0:0.2f}".format(perimeter))
message.draw(win)
message2 = Text(Point(5, 1),"The area is: {0:0.2f}".format(a))
message2.draw(win)
#### 完整代码#####
# Program: triangle.py
import math
from graphics import *
def square(x):
return x * x
def distance(p1, p2):
dist = math.sqrt(square(p2.getX() - p1.getX()) + square(p2.getY() - p1.getY()))
return dist
def perimeter():
# Calculate the perimeter of the triangle
perimeter = distance(p1,p2) + distance(p2,p3) + distance(p3,p1)
return perimeter
def area():
# Calculate the area of the triangle
base = (distance(p3, p1) * (1 / 2))
height = ((base) ** 2) - (distance(p1, p2) ** 2)
h = math.sqrt(square(height))
a = ((1/2) * (base) * h)
return a
def main():
win = GraphWin("Draw a Triangle")
win.setCoords(0.0, 0.0, 10.0, 10.0)
message = Text(Point(5, 0.5), "Click on three points")
message.draw(win)
# Get and draw three vertices of triangle
p1 = win.getMouse()
p1.draw(win)
p2 = win.getMouse()
p2.draw(win)
p3 = win.getMouse()
p3.draw(win)
# Use Polygon object to draw the triangle
triangle = Polygon(p1,p2,p3)
triangle.setFill("black")
triangle.setOutline("blue")
triangle.draw(win)
message = Text(Point(5, 0.5),"The perimeter is: {0:0.2f}".format(perimeter))
message.draw(win)
message2 = Text(Point(5, 1),"The area is: {0:0.2f}".format(a))
message2.draw(win)
# Wait for another click to exit
win.getMouse()
win.close()
main()