
Pythonを学ぼう! -Turtle編-
今日は Turtle でお絵描きダッ!
Turtle は図形で結果を表示してくれるから、コピペしてやってみるだけで結構楽しいよね
今日は def の中に def がある難しすぎる(再帰的な) プログラミングだったにょ👇
(コッホ曲線って言うらしい)
from turtle import *
def Recursive_Koch(t, length, depth):
if depth == 0:
t.forward(length)
else:
Recursive_Koch(t, length, depth-1)
t.right(60)
Recursive_Koch(t, length, depth-1)
t.left(120)
Recursive_Koch(t, length, depth-1)
t.right(60)
Recursive_Koch(t, length, depth-1)
t = Turtle()
# t.left(90)
# t.backward(300)
Recursive_Koch(t, 5, 8)
出来上がった図形はこんな感じ👇

途中で終わっちゃったんだけど…(;;)
プログラミングの最後の
Recursive_Koch(t, 5, 8)
👆この部分の (5, 8) を大きくしたり小さくしたりすると、図形の大きさも変わるらしい
「再帰」おもしろいね(?)
(アンソニー・ギデンズのモダニティについての本を読んだ後だから尚更?)