我一直试图制作一个三角形并使用Turtle来绘制它。我希望人们能够导入值,我的程序会说它是否是三角形。如果它是一个三角形,那么我的程序会把它写出来。如果没有,它将给出错误。当我测试我的代码时,它表示angle1未定义。在第56行,我尝试将角度1和所有其他参数更改为数字并且它有效,但我希望用户输入数字,而不是将它们输入到程序中。不要担心数学。我已经添加了所有代码。我只需要帮助让第56行工作。
from turtle import *
canvas = Screen()
canvas.setup(400,200)
class Triangle(object):
"""Triangle"""
sides = 3
def __init__(self, angle1, angle2, angle3, length1, length2, length3):
self.angle1 = angle1
self.angle2 = angle2
self.angle3 = angle3
self.length1 = length1
self.length2 = length2
self.length3 = length3
print "Enter the following inputs"
angle1 = raw_input("Please enter Angle 1: ")
print angle1
angle2 = raw_input("Please enter Angle 2: ")
print angle2
angle3 = raw_input("Please enter Angle 3: ")
print angle3
length1 = raw_input("Please enter Length 1: ")
print length1
length2 = raw_input("Please enter Length 2: ")
print length2
length3 = raw_input("Please enter Length 3: ")
print length3
def description(self):
print angle1
print angle2
print angle3
print length1
print length2
print length3
self.degrees()
def check_Angles(self):
"Lets Make sure the angles add up to 180"
if self.angle1 + self.angle2 + self.angle3 == 180:
print "That is a good Triangle"
return True
else:
print "That is not a good Triangle"
return False
def draw(self):
if self.check_Angles():
pen = Turtle()
pen.forward(self.length1)
pen.left(self.angle1)
pen.forward(self.length2)
pen.left(self.angle2)
pen.forward(self.length3)
triangle2 = Triangle(angle1, angle2, angle3, length1, length2, length3)
triangle2.draw()
canvas.exitonclick()
答案 0 :(得分:0)
以下重新安排代码将使其运行,但不一定能够运行。即它没有绘制正确的三角形 - 但这是你可以在现在运行的代码中柚木的东西:
from turtle import *
canvas = Screen()
canvas.setup(400, 200)
class Triangle(object):
"""Triangle Class"""
def __init__(self, angle1, angle2, angle3, length1, length2, length3):
self.angle1 = angle1
self.angle2 = angle2
self.angle3 = angle3
self.length1 = length1
self.length2 = length2
self.length3 = length3
def description(self):
print angle1
print angle2
print angle3
print length1
print length2
print length3
def check_Angles(self):
"""Lets Make sure the angles add up to 180"""
if self.angle1 + self.angle2 + self.angle3 == 180:
print "That is a good Triangle"
return True
else:
print "That is not a good Triangle"
return False
def draw(self):
if self.check_Angles():
pen = Turtle()
pen.forward(self.length1)
pen.left(self.angle1)
pen.forward(self.length2)
pen.left(self.angle2)
pen.forward(self.length3)
print "Enter the following values:"
angle1 = int(raw_input("Please enter Angle 1: "))
angle2 = int(raw_input("Please enter Angle 2: "))
angle3 = int(raw_input("Please enter Angle 3: "))
length1 = int(raw_input("Please enter Length 1: "))
length2 = int(raw_input("Please enter Length 2: "))
length3 = int(raw_input("Please enter Length 3: "))
triangle = Triangle(angle1, angle2, angle3, length1, length2, length3)
triangle.draw()
canvas.exitonclick()