2018年1月25日木曜日

開発環境

Teach Your Kids to Code: A Parent-Friendly Guide to Python Programming (Bryson Payne(著)、No Starch Press)のChapter 3.(Numbers and Variables: Python Does the Math)、PROGRAMMING CHALLENGES、#2: CUSTOM NAME SPIRALSを取り組んでみる。

#2: CUSTOM NAME SPIRALS

コード(Emacs)

Python 3

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

t = turtle.Pen()
t.speed(0)
turtle.bgcolor('black')
colors = ['red', 'yellow', 'blue', 'orange',
          'green', 'purple', 'brown', 'white', 'gray', 'pink']
your_name = turtle.textinput('Enter your name', 'What is your name?')
sides = int(turtle.numinput('Number of sides',
                            'How many sides do you want(1-8)?', 4, 1, 8))
for x in range(100):
    t.pencolor(colors[x % sides])
    t.penup()
    t.forward(x * sides)
    t.pendown()
    t.write(your_name, font=('Arial', int((x + sides) / sides), 'bold'))
    t.left(360 / sides + 2)

print('done')
while True:
    time.sleep(10)

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

$ ./sample2.py
done
  C-c C-cTraceback (most recent call last):
  File "./sample2.py", line 23, in <module>
    time.sleep(10)
KeyboardInterrupt
$ ./sample2.py
done
  C-c C-cTraceback (most recent call last):
  File "./sample2.py", line 23, in <module>
    time.sleep(10)
KeyboardInterrupt
$ ./sample2.py
done
  C-c C-cTraceback (most recent call last):
  File "./sample2.py", line 23, in <module>
    time.sleep(10)
KeyboardInterrupt
$

0 コメント:

コメントを投稿