【问题标题】:Python Sierpinski Triangle Using Class Point使用类点的 Python 谢尔宾斯基三角形
【发布时间】:2013-11-19 12:29:49
【问题描述】:

我必须使用类点来绘制谢尔宾斯基三角形,但是当我尝试运行我创建的程序时,它不会让我绘制三角形。我们必须有一个大三角形三边长度(L)和迭代次数(I)的输入函数。请帮忙!这就是我所拥有的。

import turtle

class point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def midpoint(self, p2):
        return point((self.x + p2.x) / 2, (self.y + p2.y) / 2)

def DrawSierpinskiTriangle(length, iterations):
    turn = 0
    angle = 60
    # Initialize the turtle
    turtle.hideturtle()
    turle.penup()
    turtle.degrees()
    # Starting point on the cancas
    midpoint = ((self.x + p2.x) / 2, (self.y + p2.y) / 2)
    decode = {'-':Left, '+':Right, 'X':Forward, 'H':Forward}
    code = 'H--X--X'
    # Start the drawing
    turtle.goto(point[0], point[1])
    turtle.pendown()
    turtle.hideturtle()
    turt=turtle.getpen()
    startposition=turt.clone()
    # Get triangle
    path = code
    length = x
    for i in range(0,length):
        path = path.replace('X','XX')
        path = path.replace('H','H--X++H++X--H')
    for i in path:
        [turn, point, fwd, angle, turt]=decode[i](turn, point, fwd, angle, turt)

def main():
    input("Enter the length of the triangle: ")
    input("Enter the number of iterations: ")

main()

【问题讨论】:

  • 您可能想要指定程序的更多细节,例如您正在使用 Python turtle 库来绘制三角形。您是否正确导入了turtle 库?另外,您能否更详细地描述您的代码所遇到的错误?

标签: python python-3.x


【解决方案1】:

A) 如果使用 python 2.x,您可能想要使用 raw_input

B)您没有保存输入的值。 IE。

tLength = input("Enter the length of the triangle: ")

C) 你从不打电话给DrawSierpinskiTriangle

总之,试试下面的方法,这应该会让你回到正轨,你可以开始调试你的 Sierpinski 函数

def main():
    tLength = input("Enter the length of the triangle: ")
    numIter = input("Enter the number of iterations: ")
    DrawSierpinskiTriangle(tLength, numIter)

main()

【讨论】:

  • raw_input vs. input 取决于他使用的 Python 版本。
  • @SteinarLima 确实如此,但它已被编辑以显示 python 3.x 标签,答案已更新
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多