Preparing programs

When you set a task, the actual question usually should not sit below six lines of setup. That is what a line starting with #--- is for: it separates the preparation from the visible part. What stands before it (helper functions, the world, images) folds away, and the pupils see only the simple program.

The rule

A line starting with #--- begins a new section. With a name after it, the section can be chosen. Without one, the visible part starts there.

Everything before the first separator is the preparation. It always runs, whichever section is chosen.

One separator

Whatever is above it gets folded away. The visible part starts at line 1 again – breakpoints and error messages count that way too.

world("""
####
#@.#
####
""")
#---
move()
removeBerry()

Several starting positions

Give separators a name and tabs appear above the editor. The visible part stays the same when you switch – so you try the same program in several situations. In a link, ?block=Name picks the section it starts with.

#--- Straight
world("""
######
#@ . #
######
""")
#--- With a gap
world("""
#######
#@ .  #
#   . #
#######
""")
#---
while not treeFront():
    move()
    if onBerry():
        removeBerry()

Title and task

If the preparation starts with a docstring that begins with Task:, CodeNatter shows it as a heading and task above the editor. The first line is the title, the rest is the task. In the share dialog under “Task” you can also type both – CodeNatter then writes them into the program itself. The German word Aufgabe: works just as well.

"""Task: Collect berries
Kara should walk to the tree and pick up every berry.
"""
world("""
#######
#@ . .#
#######
""")
#---
while not treeFront():
    move()

The task is plain text: line breaks stay, there is no formatting. Because it is part of the program, it travels with every link – nothing is stored.

Good to know

  • It stays real Python. #--- is an ordinary comment, the docstring a string that does nothing. Copy your program into a .py file and it runs. With several sections they run one after another, the last one wins.
  • Folded does not mean hidden. A click on the gray bar shows the preparation. It is there to keep things tidy, not to keep secrets. If an error happens in it, it unfolds by itself.
  • If you forget the last separator – the one without a name – nothing is folded and you see your whole program. Better to show too much than too little.
  • Trailing dashes are fine: #--- Straight --- works just like #--- Straight.
  • Only what is visible gets printed: a folded preparation does not appear on paper, and neither do the tabs.