主要的问题是while循环持续了无限长的时间。在#This is infinite loop because 'side' iterator is ALWAYS in the sequence returned from range function
while side in range(n):
而且,在目前的结构中
在你的代码中,这个函数什么也不做,只浪费空间(你可能从shell调用它
可以理解)。还有一些多余的人,我们也可以搭顺风车。让我们设计你的脚本,让乌龟
可以从创建的函数控制多边形。希望你能看到海龟模块有多强大
当递归和正确使用函数时可以使用。在
让我们先看一下多边形函数的去极化。我觉得你的脚本应该围绕
多边形函数除了函数的自然便利性外,还与脚本中包含的参数有关。
尽管根据脚本的隐含设计不需要它们(至少在这里),但是包含它们意味着:You
用于这个函数来控制海龟,或者B:您不太了解函数/参数是如何工作的。到
提供更多的学习经验,我们绝对应该把这个脚本集中在这个函数上。在
^{pr2}$
现在就把那些帕拉马特干掉。稍后我们将在嵌套函数中返回它们。接下来,我们将添加其余的
将脚本转换为多边形函数。因为n和length变量收集输入,所以它呈现参数
对于多边形函数没用。这不是一个非此即彼的场景,有了一些控制流,如果你
通缉犯。在我们将脚本添加到多边形函数之前,我想指出您是如何声明两次变量的
第二次你把它们转换成整数。Python允许我们在第一次声明时将它们嵌套在int()函数中
他们。在n = input("Enter num ")
n = int(n) #< -instead of these 1st 2 lines, do the 3rd line below.
n = int(input("Enter num: ")) #< 1 line of code that is equally as readable as 2.
在修改了两个n&length变量之后,让我们将所有内容添加到我们的多边形函数中(除了while循环,
处理掉所有与此相关的事情)。请注意,屏幕、形状和模式功能已移动到
可变去极化。这样,当用户输入信息时,turtle窗口不会跳到用户面前
进入程序。在def polygon():
n = int(input("Enter number of sides: "))
print("You entered %d sides.\n"%(n))
length = int(input("Enter length of sides: "))
print("Your sides are %d pixels long.\n"%(length))
Screen()
shape('turtle')
mode('logo')
现在我们有了一个干净易读的函数,让我们处理创建多边形的业务。为此我们将使用
同时使用递归和参数的嵌套函数。我们叫它“活套”。原因是你
赋值是使有边的多边形数量相等(换句话说,多边形数==n)。活套将
为我们实现这个目标。首先,它将把在多边形中建立的变量作为参数。那我们就用你以前的
内部循环。在def looper(n,length,loops=n): #notice the 'loops=n' default parameter, this allows to keep track of it recursively
if (loops > 0): #As long as loops is greater than zero this functin will repeat itself as many times as 'n'
for side in range(n):
forward(length)
right(360/n)
penup()
#penup after the forloop so that the turtle will not draw while searching for next polygon space
setposition(xcor()+length,ycor()+length) #xcor() and ycor() return the current position of the turtle
#notice that we add it to the length of of our sides, this is how the turtle will space out the polys.
#I would even experiment with things like setposition(xcor()+(length*2),ycor()+(length*2))
pendown() #after turtle find the position we we use pendown() to prepare to draw again
loops -= 1 #this decrements the loops parameter so that the functin will not call itself infinitely
#(stack overflow)
looper(n,length,loops) #recursively call our looper function again
return #I personally always return the end of recursive functions to be safe, this may be redundant
本质上,递归是指函数在自身内部反复调用自身以执行任务。以确保
最后,我们告诉程序:“只有当有任何循环时才画多边形”在函数执行之后
我们的职责是告诉它“将循环减去1”,以确保循环最终为零。这和回报
声明(大致相当于您所说的“中断”)将确保我们不会执行任何任务
无数次。此代码的最后一步是确保实际调用polygon函数
因此,您的代码将运行,并调用looper(n,length)和多边形函数的结尾,原因是相同的。在
您的代码应该如下所示:from turtle import *
def polygon():
n = int(input("Enter number of sides: "))
print("You entered %d sides.\n"%(n))
length = int(input("Enter length of sides: "))
print("Your sides are %d pixels long.\n"%(length))
Screen()
shape('turtle')
mode('logo')
def looper(n,length,loops=n):
if (loops > 0):
for side in range(n):
forward(length)
right(360/n)
penup()
setposition(xcor()+length,ycor()+length)
pendown()
loops -= 1
looper(n,length,loops)
return
looper(n,length)
polygon()
我几乎为你完成了任务,但如果你学到了一两件事,那么我的目标就实现了。我希望我能帮助任何人!在