Clock

Shows today's date, greets you by the time of day and then runs a live clock with hands until you press Space. Teaches working with the time, your own functions, sin and cos, drawing lines and a loop that waits for a key.

Open in Tokbit Level: Intermediate. Free in the browser, nothing to install, no account needed.

Run the example in Tokbit and step through it: before each step say what will happen and check it in the Variables panel. Then change it your way.

Flowchart

Flowchart of the Clock example

Python program

The same program as Python code. In Tokbit you see it when you switch from Diagram to Python above the flowchart.

"""Today's date, a greeting by the hour and a live clock with hands. Teaches hour(), minute(), second(), your own functions, sin and cos and a loop that ends with a key."""
from math import sin, cos, radians
# Tokbit screen functions, not in plain Python:
from tokbit import (
    cls, at, color, key, year, month, day, hour, minute, second, sleep, line,
    circle, WHITE, BLUE, LIGHT_RED, LIGHT_BLUE,
)

def two_digits(n: int) -> str:
    """A number with two digits: 7 → "07"."""
    if n < 10:
        return "0" + str(n)
    return str(n)

def greeting(h: int) -> str:
    """A greeting by the hour: night before 5 and from 22, morning until 12, afternoon until 18, then evening."""
    if h < 5 or h >= 22:
        return "Good night!"
    if h < 9:
        return "Good morning!"
    if h < 12:
        return "Good morning!"
    if h < 18:
        return "Good afternoon!"
    return "Good evening!"

def ray(angle: float, start: int, length: int):
    """A line in direction angle (in degrees, 0 = up, clockwise) from the clock centre (240, 96): from distance start to length."""
    a: float = radians(angle)
    # sin gives the step to the right, cos the step up; screen y grows downwards, so it is 96 minus.
    line(240 + round(start * sin(a)), 96 - round(start * cos(a)), 240 + round(length * sin(a)), 96 - round(length * cos(a)))

def dial():
    """The dial: a circle and the hour marks."""
    color(WHITE)
    circle(240, 96, 44)
    # Twelve marks, 30° apart.
    for mark in range(12):
        ray(mark * 30, 39, 43)
    color(LIGHT_BLUE)

def hands():
    """The hands at the current time."""
    color(BLUE)
    # Clears the old hands: a filled circle in the background colour.
    circle(240, 96, 37, True)
    color(WHITE)
    # The hour hand: 30° per hour and half a degree per minute.
    ray(hour() % 12 * 30 + minute() / 2, 0, 22)
    ray(minute() * 6, 0, 32)
    color(LIGHT_RED)
    # The second hand: 6° per second.
    ray(second() * 6, 0, 36)
    color(LIGHT_BLUE)

cls()
print("Today is " + str(day()) + "/" + str(month()) + "/" + str(year()))
print(greeting(hour()))
print("Space (Action) stops the clock.")
dial()
k: str = ""
# A live clock: the hands and the time are redrawn until you press Space.
while k != " ":
    hands()
    at(4, 12)
    print(two_digits(hour()) + ":" + two_digits(minute()) + ":" + two_digits(second()), end="")
    sleep(200)
    k = key()

Follow-up tasks

Tasks in which you change the example (with a model solution):

All examples