Today I discovered the turtle library for python.

 If you are old enough to remember the educational programming language Logo, then you might remember spending hours in your school’s BBC microcomputer lab playing with this electronic turtle.

 Logo provides a way of drawing line graphics and patterns using simple commands such as ‘forward’ and ‘right’. Logo is a good way of teaching and learning ‘Computational Thinking’ or the basics of programming. Today you can experience the same fun on your Raspberry Pi or Python interpreter on your PC, as the turtle library is included in the standard distribution.

 After playing with a few random walks (which I may post at a later date), I had a go at creating a working analogue clock, and I am rather pleased with the results. Thanks to Sonny for his enthusiasm and suggested improvements.

Python 3 analogue clock using the turtle graphics library. 

 You can download the code, or copy it from below.

#Turtle Analogue Clock
#Tim Street
#version 1.6

 import turtle
import time

 print("Python Turtle Analogue Clock")
print("By T Street")

 #Deal with different time zones
ok = False
while not(ok):
print("\nFor example, for British summer time enter 1")
offset = int(input("Enter offset from GMT (-11 to 11) :"))
if offset >= -11 and offset <= 11:
ok = True

wn = turtle.Screen()
wn.title("TURTLE CLOCK")

 SCALE = 1.7 # size of clock scale factor (try 2.0 to 0.5)

 #create dial
mark = turtle.Turtle()
for i in range(60):
 if i % 5 == 0:

 update = True #controls whether minute and hour hand should update (once per minute)
updateSecond = True # controls whether the second hanbd should update
while True: 
 b = time.gmtime(time.time()) # current GMT
 m = b.tm_min # remember the current minute
 s = b.tm_sec # rember the current second
 if update:
 #hour hand
 hour = turtle.Turtle()
 hour.right(((b.tm_hour + offset) % 12) * 30 + b.tm_min * 0.5 )

 #minute hand
 minute = turtle.Turtle()
 minute.right((b.tm_min) * 6)

 update = False
 if updateSecond:
 #second hand
 second = turtle.Turtle()
 second.right((b.tm_sec) * 6)
 updateSecond = False

 b = time.gmtime(time.time())
 new_min = b.tm_min
 new_sec = b.tm_sec

 if new_min != m:
 update = True
 hour.clear() # Clear out the drawing (if any)
 if new_sec != s:
 updateSecond = True

Originally published on Blogger

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.