2018年1月19日金曜日

開発環境

Teach Your Kids to Code: A Parent-Friendly Guide to Python Programming (Bryson Payne(著)、No Starch Press)のChapter 2.(Turtle Graphics: Drawing with Python)、PROGRAMMING CHALLENGES、#1: CHANGING THE NUMBER OF SIDESを取り組んでみる。

#1: CHANGING THE NUMBER OF SIDES

コード(Emacs)

Python 3

#!/usr/bin/env python3
import sys
import time
import turtle

t = turtle.Pen()
t.speed(0)
turtle.bgcolor('black')

sides = int(sys.argv[1])

colors = ['red', 'yellow', 'blue', 'orange',
          'green', 'purple', 'brown', 'white', 'grey', 'pink'][:sides]

for x in range(360):
    t.pencolor(colors[x % sides])
    t.forward(x * 3 / sides + x)
    t.left(360 / sides + 1)
    t.width(x * sides / 200)

print('done')

while True:
    time.sleep(10)

入出力結果(Terminal, Jupyter(IPython))

$ ./sample1.py 5
done
  C-c C-cTraceback (most recent call last):
  File "./sample1.py", line 23, in <module>
    time.sleep(10)
KeyboardInterrupt
$ ./sample1.py 4
done
  C-c C-cTraceback (most recent call last):
  File "./sample1.py", line 23, in <module>
    time.sleep(10)
KeyboardInterrupt
$ ./sample1.py 3
done
  C-c C-cTraceback (most recent call last):
  File "./sample1.py", line 23, in <module>
    time.sleep(10)
KeyboardInterrupt
$ ./sample1.py 2
done
  C-c C-cTraceback (most recent call last):
  File "./sample1.py", line 23, in <module>
    time.sleep(10)
KeyboardInterrupt
$ ./sample1.py 1
done
  C-c C-cTraceback (most recent call last):
  File "./sample1.py", line 23, in <module>
    time.sleep(10)
KeyboardInterrupt
$ ./sample1.py 8
done
  C-c C-cTraceback (most recent call last):
  File "./sample1.py", line 24, in <module>
    time.sleep(10)
KeyboardInterrupt
$ ./sample1.py 10
done
  C-c C-cTraceback (most recent call last):
  File "./sample1.py", line 24, in <module>
    time.sleep(10)
KeyboardInterrupt
$ 

0 コメント:

コメントを投稿