Melody

Plays Ode to Joy from a list of notes and lengths. Teaches playing notes, lists and looping over indexes.

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 Melody example

Python program

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

"""Plays the start of Ode to Joy. Teaches two lists of the same length."""
# Tokbit screen functions, not in plain Python:
from tokbit import note

print("Ode to Joy (Beethoven)")
notes: list[str] = ["E4", "E4", "F4", "G4", "G4", "F4", "E4", "D4", "C4", "C4", "D4", "E4", "E4", "D4", "D4", "E4", "E4", "F4", "G4", "G4", "F4", "E4", "D4", "C4", "C4", "D4", "E4", "D4", "C4", "C4"]
lengths: list[int] = [400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 600, 200, 800, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 400, 600, 200, 800]
# The same index i picks both the note and its length.
for i in range(len(notes)):
    print(notes[i] + " ", end="")
    note(notes[i], lengths[i])
print("")

Follow-up tasks

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

All examples