爲了與龜使用遞歸得出這樣的:Python的遞歸函數龜,吸引資本我的
但我真的不擅長這個。這就是我得到:
下面的代碼: https://gyazo.com/24cebddbb111506fd6959bb91dadb481
import turtle
def draw_shape(t, level,size):
if level == 1: #draws an I
t.down()
t.forward(size/2)
t.left(90)
t.forward(size/2)
t.back(size)
t.forward(size/2)
t.left(90)
t.forward(size)
t.left(90)
t.forward(size/2)
t.back(size)
t.up()
t.forward(size/2)
t.left(90)
t.forward(size/2)
else:
draw_shape(t,level - 1,size)
t.back(size/2)
t.right(90)
t.forward(size/2)
t.left(90)
draw_shape(t,level - 1,size/2)
t.left(90)
t.forward(size)
t.right(90)
draw_shape(t,level-1,size/2)
t.right(90)
t.forward(size/2)
t.left(90)
t.forward(size)
t.right(90)
t.forward(size/2)
t.left(90)
draw_shape(t,level-1,size/2)
t.left(90)
t.forward(size)
t.right(90)
draw_shape(t,level-1,size/2)
def q1():
my_win = turtle.Screen()
my_turtle = turtle.Turtle()
my_turtle.speed(0.006)
my_turtle.left(90)
my_turtle.up()
my_turtle.back(200)
n = int(input('Draw shape at level: '))
draw_shape(my_turtle,n,200)
my_win.exitonclick()
q1()
差不多隻是爲了編輯draw_shape()函數。我在第2級正確地做到了,但其餘的關卡開始變得亂七八糟,並且在錯誤的位置上繪製了錯誤的大小,我認爲這是因爲我在繪製完成後放置了指針。任何幫助將不勝感激。
你可以在問題中添加代碼作爲文本嗎?我們無法複製和粘貼屏幕截圖。 – Blckknght
不知道如何感謝溢出,但感謝您幫助我的第一篇文章,你爲我節省了很多頭髮,因爲我不知道如何在每次打電話後讓筆回到中心。絕對需要經過幾次才能得到遞歸的結果。再次感謝我真的很感激它:) – thecylax