Strict about the rules
A text-based language is very strict. One missing bracket, a misspelt word or a capital letter in the wrong place can stop the whole program. A mistake against the writing rules of the language, such as a missing bracket or quotation mark, is called a syntax error. A misspelt command, such as prnt, also stops the program, because the computer does not know that word. Brackets and quotation marks come in pairs, and each one that opens must be closed. Python also uses indentation: the lines that are indented under a for line are repeated, and the others are not. When the computer finds an error, it prints an error message with a line number. We read the message, look at that line and the line before it, and fix the mistake. In a block language many of these mistakes cannot happen, because the blocks only fit together in the right ways.
1name = "Ravi" 2print("Hello " + name)
Hello Ravi
| Line | Mistake | Fixed line |
|---|---|---|
| prnt("Hi") | print is misspelt | print("Hi") |
| print("Hi) | the closing quotation mark is missing | print("Hi") |
| print "Hi" | the brackets are missing | print("Hi") |
1for i in range(3): 2print("Go!")
IndentationError: expected an indented block
| Mistake | In a text-based language | In a block-based language |
|---|---|---|
| misspelt command | an error: the word is unknown | not possible, we pick the block |
| missing bracket | syntax error | not possible, the slots are fixed |
| wrong order of steps | bug that is hard to see | bug, but the order is easy to see |
Strict robot (no computer): make up a tiny robot language with commands such as FORWARD(2), TURN(RIGHT) and TURN(LEFT). The rules: capital letters, brackets and one command per line. One player is the strict robot on a floor grid made with paper squares. A friend writes a five-line program for the robot to go from a start square to a treasure square. The robot follows only lines that are written exactly right. If a line breaks the rules, the robot says ‘syntax error on line 3’ and stops. Then the writer fixes it. Swap roles. If you have a computer or tablet, watch an adult run a very short program and read an error message.
1Print("Good morning") 2print("Good night"
1for i in range(2): 2 print("Hi") 3print("Bye")