The IDE

Editors, error diagnostics, the run-time environment and the translator, in the cell you use.

F6.6Robust programsGCSE12 min

Do this lesson in the simulator

You have been using an integrated development environment, or IDE, since your first lesson: every Try-it cell is one. An IDE puts the tools a programmer needs in one place, so writing, running, testing and fixing a program all happen in one window. Professional IDEs such as VS Code and PyCharm have far more features, but the core ones are the same, and they are all in the cell in front of you.

The editor

The editor is where you write code, and it does more than a plain text box:

  • Line numbers, so an error message's line is easy to find.
  • Syntax highlighting: keywords, strings, numbers and comments in different colours, so a missing quote shows up at once as everything after it changes colour.
  • Automatic indentation: press Enter after a colon and the next line is indented for you.
  • Code completion: type two letters of a name and a list of matching commands appears. Press Ctrl and Space to open it any time.
  • Folding: the arrows beside def lines hide a function's body, so a long program is easier to read.

Try each one in this cell. Type for on a new line and watch the colours, press Enter after a colon, and type fo to see the suggestions:

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

def blink(colour):
    led(colour)
    wait(0.3)
    led("off")

blink("green")

Run this in the simulator

Error diagnostics

An IDE finds some errors before you even run the program, and explains the rest when you do:

  • A squiggle under a line means Python cannot understand it: a syntax error, found as you type. Hover over it to read why.
  • A runtime error stops the program, shows the error message in red in the console, and highlights the line where it happened.

This cell has one of each. Find the squiggle first, fix it, then run and read the error that is left:

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

for i in range(3)
    led("blue")
    wait(0.3)
print("done after", count, "flashes")

Run this in the simulator

Running and debugging

  • Run is the run-time environment: the IDE runs your program and shows the result, here the robot on the mat and the console. You never leave the editor to see it work.
  • Debug runs the program one line at a time. The line about to run is highlighted, and the current values of the variables are shown beside the controls. Step runs one line, Run on carries on normally, and Stop ends the run.
  • Replay plays the last run again, and the slider moves through it, so you can look closely at the moment something went wrong.

Press Debug on this cell and step through it, watching total change:

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

total = 0
for reading in [40, 35, 30]:
    total = total + reading
average = total / 3
print("average", average)

Run this in the simulator

The translator

Every IDE includes, or connects to, a translator: the compiler or interpreter from lesson F6.5. Here it is a Python interpreter running inside your browser. When you press Run, the IDE hands your program to the interpreter; its error messages are what the diagnostics show.

The rest of the toolbox

The buttons above each cell are IDE features too: saved versions keep earlier copies of your program to go back to, reference lists every command with an example, download saves the program as a .py file to open in another IDE, and full screen gives the editor room for a longer program. The BugBot Simulator page is the same IDE on a whole page, with files of your own.

Task: use the diagnostics

This program should flash the LED three times, beeping each time, and then print flashed 3 times. It has a syntax error, a runtime error and a logic error. Use the squiggle, the error message and Debug to find and fix all three.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

flashes = 0
for i in range(3)
    led("blue")
    tone(660, 0.1)
    wait(0.2)
    led("off")
    flashes = 1
print("flashed", flashs, "times")

Challenges

  1. Download a program from a cell as a .py file and open it in another editor. Which features are missing?
  2. Use the saved versions button to keep a working copy of a program, break it on purpose, then go back.
  3. List the IDE features you used in this lesson under OCR's four headings: editors, error diagnostics, run-time environment and translators.