我试图测试我的三角形类,特别是在draw_triangle函数但是一直得到错误:"非绑定方法forward()必须使用Turtle实例作为第一个参数调用(改为使用int实例)& #34 ;.我该如何解决这个问题?
from __future__ import print_function, division
from math import sqrt, degrees, acos
import turtle
class Triangle():
def __init__(self, a = None, b = None, c = None):
self.a = a;
self.b = b;
self.c = c;
def is_triangle(self):
s = 0.5 * (self.a + self.b + self.c)
return (s - self.a) > 0 and (s - self.b) > 0 and (s - self.c) > 0
def perimeter(self):
return self.a + self.b + self.c
def area(self):
s = 0.5 * (self.a + self.b + self.c)
return sqrt(s * (s - self.a) * (s - self.b) * (s - self.c))
def a_angle(self):
return degrees(acos((self.b ** 2 + self.c ** 2 - self.a ** 2) / (2.0 * self.b * self.c)))
def b_angle(self):
return degrees(acos((self.a ** 2 + self.c ** 2 - self.b ** 2) / (2.0 * self.a * self.c)))
def c_angle(self):
return degrees(acos((self.a ** 2 + self.b ** 2 - self.c ** 2) / (2.0 * self.a * self.b)))
def angles(self):
angA = a_angle(self)
angB = b_angle(self)
angC = c_angle(self)
return angA, angB, angC
def __str__(self):
return 'lengths = %d, %d, %d' % (self.a, self.b, self.c) + \
', perimeter = %d' % (perimeter(self)) + \
', area = %d' % (area(self)) + ', angles = %d' % (angles(self))
def draw_triangle(self):
window = turtle.Screen()
t = turtle.Turtle
t.forward(self.c)
t.left(180 - a_angle(self))
t.forward(self.a)
t.left(180 - b_angle(self))
t.forward(self.b)
t.done()
window.exitonclick()
if __name__ == '__main__':
triangle1 = Triangle(100,100,72)
triangle2 = Triangle(100,100,100*sqrt(2))
triangle3 = Triangle(100,50,50)
print (triangle1)
print (triangle2)
print (triangle3)
Triangle.draw_triangle(triangle1)
Triangle.draw_triangle(triangle2)
Triangle.draw_triangle(triangle3)
turtle.mainloop()
答案 0 :(得分:1)
这似乎有效:
from __future__ import print_function, division
from math import sqrt, degrees, acos
import turtle
class Triangle():
def __init__(self, a = None, b = None, c = None):
self.a = a;
self.b = b;
self.c = c;
def is_triangle(self):
s = 0.5 * (self.a + self.b + self.c)
return (s - self.a) > 0 and (s - self.b) > 0 and (s - self.c) > 0
def perimeter(self):
return self.a + self.b + self.c
def area(self):
s = 0.5 * (self.a + self.b + self.c)
return sqrt(s * (s - self.a) * (s - self.b) * (s - self.c))
def a_angle(self):
return degrees(acos((self.b ** 2 + self.c ** 2 - self.a ** 2) / (2.0 * self.b * self.c)))
def b_angle(self):
return degrees(acos((self.a ** 2 + self.c ** 2 - self.b ** 2) / (2.0 * self.a * self.c)))
def c_angle(self):
return degrees(acos((self.a ** 2 + self.b ** 2 - self.c ** 2) / (2.0 * self.a * self.b)))
def angles(self):
angA = self.a_angle()
angB = self.b_angle()
angC = self.c_angle()
return angA, angB, angC
def __str__(self):
return 'lengths = %d, %d, %d' % (self.a, self.b, self.c) + \
', perimeter = %d' % (self.perimeter()) + \
', area = %d' % (self.area()) + ', angles = %d %d %d' % (self.angles())
def draw_triangle(self):
window = turtle.Screen()
t = turtle.Turtle
t.forward(self.c)
t.left(180 - self.a_angle(self))
t.forward(self.a)
t.left(180 - self.b_angle(self))
t.forward(self.b)
t.done()
window.exitonclick()
if __name__ == '__main__':
triangle1 = Triangle(100,100,72)
triangle2 = Triangle(100,100,100*sqrt(2))
triangle3 = Triangle(100,50,50)
print (triangle1)
print (triangle2)
print (triangle3)
你需要"自我。"在方法调用前面
这是一个稍微修改过的draw_triangle函数。它运行,但可能并不完全符合您的要求。
def draw_triangle(self):
window = turtle.Screen()
t = turtle.Turtle()
t.forward(self.c)
t.left(180 - self.a_angle())
t.forward(self.a)
t.left(180 - self.b_angle())
t.forward(self.b)
turtle.done()
window.exitonclick()
我认为你现在应该自己动手。