2016-03-06 69 views
0

我編寫了完成所需任務的任務代碼。從用戶輸入中繪製多邊形的形狀,並繪製每個指定邊數的形狀數量。唯一的問題是繪製形狀之後,箭頭只能在無限循環中追蹤。我嘗試在最後使用break,但它不起作用。如何破解Turtle Graphics Python中的無限循環

from turtle import * 

def polygon(n, length): 
    Screen() 
    shape('turtle') 
    mode('logo') 

n = input ("Enter number of sides ") 
print ("you entered ", n) 
length = input("Enter the length of sides ") 
print ("you entered ", length) 
n=int(n) 
length=int(length) 


for side in range(n): 
    forward(length) 
    right(360/n) 
    while side in range(n): 
     right(360/n) 
     for side in range(n): 
      forward(length) 
      right(360/n) 

到目前爲止,我在技術上對分配有用,但最後的無限循環令我煩惱。

+0

我確定這個循環從未結束! – Arman

+0

請不要** [cross-post](http://meta.stackexchange.com/tags/cross-posting/info)**:http://programmers.stackexchange.com/q/311913 – 2016-03-06 19:52:00

回答

0

主要問題是while循環會花費無限的時間。

#This is infinite loop because 'side' iterator is ALWAYS in the sequence returned from range function 
while side in range(n): 

此外,與您的代碼的現有結構 的函數什麼都不做,只浪費空間(你可能從哪個是 理解外殼稱呼它)。還有一些我們可以乘坐的冗餘。讓我們設計你的腳本,讓龜的 多邊形可以從你創建的函數進行控制。希望你會看到烏龜模塊 在使用遞歸和正確使用函數時有多強大。

讓我們來看看你的多邊形函數1st的衰減。我覺得你的腳本應該圍繞你的 多邊形函數,除了函數的自然便利性之外,是因爲你包含在腳本中的參數。 雖然它們並不是必需的(這裏是至少),但是包含它們意味着A:你想用這個函數來控制海龜,或者B:你不太明白函數/參數是如何工作的。到 提供了更多的學習經驗,無論是哪種情況,我們都應該將此腳本集中在該功能上。

def polygon(n,length): #<--- get ride of 'n' & 'length' parameters 
def polygon(n=None,length=None): #<---These are defualt values if you wish to go with that direction 
def polygon(): #<---This is the declaration I'll follow for aiding your script 

擺脫現在的參數。稍後我們將把它們帶回嵌套函數。接下來,我們將添加腳本的其餘 到多邊形函數。因爲你的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 &長度變量後,讓我們來添加一切都變成了多邊形功能(除了while循環, 擺脫參與這一切的)。請注意,屏幕,形狀和模式功能已被移動到低於變量的可變範圍以下。這是因爲烏龜窗口在用戶輸入信息 時不會跳到用戶面前。

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') 

既然我們有一個乾淨可讀的函數,可以通過創建多邊形來處理我們的業務。爲此,我們將使用 嵌套函數,該函數使用遞歸和參數。我們將其稱爲'looper'。原因在於你的 賦值是使等邊的多邊形(換言之,多邊形的數目== n)。活套將 爲我們實現。第一,它將以多邊形中建立的變量作爲參數。然後我們將使用您以前的 for循環裏面。

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」以確保循環最終爲零。這一點和返回 聲明(大致相當於您所稱的「休息」)將確保我們不會執行任務 無限次的任務。這段代碼的最後一步是確保實際調用多邊形函數 ,以便您的代碼運行AND也可以調用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() 

我非常爲你做的任務,但如果你學會了兩件事,然後實現我的目標。我希望我能幫助任何人!

+0

感謝您採取要評論的時間,但恐怕你的回答不是我要找的。正如我所說,我的代碼完成了任務的任務。在python中運行它,你會看到它從作業本身產生的圖片中顯示的內容:http://imgur.com/7MkpmVp – user6025404