2012-09-15 62 views
0

我想創建一個循環廣場,並不能弄清楚如何讓我的代碼,讓我不斷重複創建正方形,倍數輸入的命令,繼承人我目前有什麼。蟒龜龜環

square_count = input("Enter the number of squares to draw: ") 
count_int = int(square_ct) 

if count_int > 1: 

    turtle.begin_fill() 
    turtle.forward(100) 
    turtle.right(90) 
    turtle.forward(100) 
    turtle.right(90) 
    turtle.forward(100) 
    turtle.right(90) 
    turtle.forward(100) 
    turtle.end_fill() 

    turtle.up() 
    turtle.forward(20) 
    turtle.color(random.random(),random.random(), random.random()) 

回答

3

您可以使用for i in range(count_int):運行一段代碼多次給予重複計數count_int

if count_int > 1: 
    for i in range(count_int): 
     turtle.begin_fill() 
     turtle.forward(100) 
     turtle.right(90) 
     turtle.forward(100) 
     turtle.right(90) 
     turtle.forward(100) 
     turtle.right(90) 
     turtle.forward(100) 
     turtle.end_fill() 

     turtle.up() 
     turtle.forward(20) 
     turtle.color(random.random(),random.random(), random.random()) 
+0

有沒有更好的方式來寫'turtle.forward(100) turtle.right( 90) turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.right(90) turtle.forward(100)',因爲它們是相同的值重複多次? – DebRaj

+0

@DebRaj:請參閱[Python 3:我們可以避免在調用其幾個方法時重複實例名稱?](https://stackoverflow.com/q/35407914),這可能是您爲什麼首先發布您的評論的原因。 –

+0

@Martin Pieters鏈接是關於不同的運動。我希望它能成爲類似的動作聲望。我發現定義一個方法是有幫助的,例如:'draw draw_square(some_draw):for i in range(0,4):some_draw.forward(100)some_draw.right(90)',然後調用它爲'draw_square(my_drawing_name)會做的伎倆。 – DebRaj

0

你可以嘗試這樣做

x=1 
while x < 10000000: 

當你做這樣的東西,你在完成10000000次之後再次輸入。 在比賽的最後階段,你必須把這個。

x+=1 

繼承人是例子,我做。

import turtle 
bob = turtle.Turtle() 
wn = turtle.Screen() 
bob.color("white") 
bob.speed(1000000000000000000000000) 
wn.bgcolor("black") 
x=1 
while x < 10000000: 
bob.forward(90) 
bob.left(89) 
bob.forward(1+x) 
-1

話又說回來了,你可以把它放在一個函數,並告訴它再次運行本身

def example(): 
    [insert code] 
    example() 
+0

你會建議如何防止它無限執行? – JohnB