如何简化我的代码?

时间:2013-10-08 03:17:44

标签: python recursion turtle-graphics simplify

我使用递归来编写一个类的程序,以模仿某些简单的分支结构,如树。在我向教授展示之前,我认为我的代码很棒。他说我的代码太复杂了,并说我需要简化它。除了将它们分开外,我不确定我还能做些什么。有小费吗? (我是一个初学者,所以对我很容易。)这个程序创建了多个不同厚度,分支数量和不同坐标的树。

import random 
import turtle
##I'm using a python module called turtle to visualize results
p1 = turtle.Pen()
##Creates a pen
p1.tracer(True)
## Shows pen drawing
p1.up()
p1.left(90)

d=random.randint(0,2)
## Varying thickness of branch
length=150
##Length of branches
contract=random.uniform(.5,1)
## Varying degree of contraction
branch=random.randint(5,8)
## Varying amount of branches
first=random.randint(30,70)
## Varying first degree of branch
next=random.randint(1,30)
## Varying degree between each branches
number1=random.randint(10,20)
number2=random.randint(-100,100)
number3=random.randint(-100,100)
# Range of numbers used for coordinates 
def drawFern1(pen, depth, length, contractBy, branches, firstBranchAngle, nextBranchAngle):
    if depth > 0:
       #Pen's Position and heading
       heading = pen.heading()
       position = pen.position()
       pen.width(depth)
       pen.forward(length)
       pen.left(firstBranchAngle)
       for i in range(branches):
        drawFern1(pen, depth-1, contractBy*length, contractBy,branches,firstBranchAngle,nextBranchAngle)
        pen.right(nextBranchAngle)
      pen.setheading(heading)
      pen.setposition(position)
# Ensures that multiple trees are created each at different coordinates. 
for i in range(number1):
   p1.sety(number2)
   p1.setx(number3)
   p1.down()
   drawFern1(p1,d,length,contract,branch,first,next)
   number2 = random.randint(-100,100)
   number3 = random.randint(-100,100)
   p1.up()

2 个答案:

答案 0 :(得分:0)

这段代码对我来说非常可靠,特别是对于Python初学者。我看到的情况要糟糕得多。

如果我正在编写它,我想我只在主number2循环内计算number3for - 这里的启动定义通常很方便{ {1}}循环,但在这种情况下不是必需的。我还会尝试使用更多解释性变量名,并且根据问题陈述,我可能要求随机生成的while值至少为1 - 如果depth生成为0,则不会绘制任何内容

我的版本看起来像这样:

depth

除了将x和y坐标随机化移动到主循环中,在文件前面移动递归函数定义,并使用一些更明确的变量名称之外,我还使用import random import turtle def drawFern(pen, depth, length, contraction, branches, firstBranchAngle, nextBranchAngle): if depth > 0: # Pen's Position and heading heading = pen.heading() position = pen.position() pen.width(depth) pen.forward(length) pen.left(firstBranchAngle) for i in xrange(branches): drawFern(pen, depth-1, contraction*length, contraction, branches, firstBranchAngle, nextBranchAngle) pen.right(nextBranchAngle) pen.setheading(heading) pen.setposition(position) # I'm using a python module called turtle to visualize results # Creates a pen pen = turtle.Pen() # Shows pen drawing pen.tracer(True) pen.up() pen.left(90) # Configure initial state # Varying depth of recursive fern depth = random.randint(1,2) # Length of branches length = 150 # Varying degree of contraction contraction = random.uniform(.5,1) # Varying number of branches branches = random.randint(5,8) # Varying first degree of branch first_angle = random.randint(30,70) # Varying degree between each branches next_angle = random.randint(1,30) number_of_trees =random.randint(10,20) for i in xrange(number_of_trees): new_x = random.randint(-100, 100) new_y = random.randint(-100, 100) pen.setx(new_x) pen.sety(new_y) pen.down() drawFern(pen, depth, length, contraction, branches, first_angle, next_angle) pen.up() 调用而不是{{ 1}}调用 - 如果你使用的是Python 2.x,那就是一个简单的优化。如果您使用的是Python 3,xrange是正确的。但这些都是微小的变化。

你也可以在range循环之前输入一个range子句甚至不尝试if等于1 - 这是另一个小优化,尽管不会产生很大的差异

答案 1 :(得分:0)

  

在我向教授展示之前,我认为自己的代码很棒。他告诉我   代码太复杂了,说我需要简化它。

考虑到绘制的树的质量,这是相当复杂的代码:

enter image description here

仅绘制垂直线和空白屏幕在编写的程序随机参数内!让我们重新设计该程序,将一些随机性从静态配置代码移到递归例程本身中。我们还将微调随机范围并清理代码,主要是通过消除仅设置和使用一次的变量:

from random import randint, uniform
from turtle import Screen, Pen  # Using python turtle module to visualize results

# Configure initial state

DEPTH = randint(3, 4)  # Varying thickness and splitting of branches
LENGTH = randint(125, 150)  # Length of branches
CONTRACT_BY = uniform(0.4, 0.8)  # Varying degree of contraction

def drawFern(pen, depth, length, contractBy):
    if depth < 1:
        return

    # Save pen's position and heading
    heading = pen.heading()
    position = pen.position()

    pen.width(depth * 1.5)  # pen thickness depends on branching
    pen.forward(length)
    pen.left(randint(30, 70)) # Varying first degree of branch)

    for _ in range(randint(5, 8)):  # Varying amount of branches
        drawFern(pen, depth - 1, contractBy * length, contractBy)
        pen.right(randint(5, 30))  # Varying degree between each branches

    # Restore pen's Position and heading
    pen.setheading(heading)
    pen.setposition(position)

screen = Screen()

pen = Pen(visible=False)
pen.left(90)

screen.tracer(False)
# Ensure that multiple trees are created each at different coordinates.
for i in range(randint(10, 20)):
    pen.penup()
    pen.setposition(randint(-200, 200), randint(-300, 0))
    pen.pendown()
    drawFern(pen, DEPTH, LENGTH, CONTRACT_BY)
screen.tracer(True)

screen.mainloop()

enter image description here

相关问题