2016-03-05 74 views
-1

I need 6 pentagon 5 small ones(inner) and 1 large one(outer), those 6 pentagon's 5 sides should be in 5 different colors我是新的python編程,前幾天我學會了嵌套for循環在python中。我討厭python中的5 x 5 x 5五邊形

的事情是,當我運行我的代碼循環花費如此多的時間停下來,我恨,我想我的程序只畫出5小五邊形有5種不同的顏色和我的代碼是這樣的:

import turtle; 
count = 5; 
turtle.speed(5); 
for steps in range(count) : 
    for color in ['blue','red','green','pink','yellow'] : 
    turtle.color(color); 
    turtle.forward(100); 
    turtle.right(360/count); 
    for moreSteps in range(color) : 
     for color in ['blue','red','green','pink','yellow'] : 
     turtle.color(color); 
     turtle.forward(50); 
     turtle.right(360/count); 
turtle.done() 

請有人幫助我得到這個謝謝你。

+0

它現在可以工作嗎? – zondo

+0

該代碼無法運行。確保你正確粘貼你的縮進*,以便我們看到。 –

+0

一個問題是'範圍(顏色)'。 'range'函數需要一個整數參數,但'color'是一個字符串,所以這會給出一個錯誤。發佈的代碼中也有一些縮進錯誤。 –

回答

0

所有的評論都很有幫助。將顏色放在最外面的循環中,以便每個五邊形保持不變。然後避免在每個五邊形之後移動一點,以便繪製在之前五邊形的頂部。例如:

import turtle 
count = 5 
turtle.speed(5) 

def drawfivepentagons(size=100): 
    for color in ['blue','red','green','pink','yellow'] : 
     turtle.color('white') 
     turtle.left(60) 
     turtle.forward(size*2) 
     turtle.color(color) 
     for steps in range(count) : 
      turtle.forward(size) 
      turtle.right(360/count) 


drawfivepentagons(100) 
turtle.forward (20) 
drawfivepentagons(50) 

turtle.done()