turtle模塊的使用練習(xí)(例子是書本的)
2020-03-25 14:51 作者:一心想當網(wǎng)紅的李老師 | 我要投稿
今天學(xué)習(xí)了一下《head first 編程之旅 python語言描述》這本書的函數(shù)、模塊、類和對象的內(nèi)容。
因為最初學(xué)的是C語言,所以沒有這方面的概念。雖然感覺和C的數(shù)據(jù)結(jié)構(gòu)有點類似,但是還是存在區(qū)別的。所以使用了一下書本的例子,稍微有點感覺。
import turtle
slowpoke=turtle.Turtle()
slowpoke.shape("turtle")
pokey=turtle.Turtle()
pokey.shape("turtle")
pokey.color('red')
####定義一個函數(shù)
def make_square(the_turtle): ?
? ? for i in range (0,4):
? ? ? ? the_turtle.forward(100)
? ? ? ? the_turtle.right(90)
###定義第二個函數(shù),并且套用了第一個函數(shù)。這次總算理解了一點,函數(shù)套用的方法。
def make_sprial(the_turtle):
? ? for i in range (0,36):
? ? ? ? make_square(the_turtle)
? ? ? ? the_turtle.right(10)
make_sprial(slowpoke)
pokey.right(5)
make_sprial(pokey)
turtle.mainloop()
標簽: