AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Languages for robots

Week 26 · Unit 11: Autonomous robots · 3 lessons
2

Strict about the rules

Computing 6: Unit 11, pp. 150–161
GoalExplain why syntax matters in a text-based language and find and fix simple syntax errors.
New words
syntax error · a mistake against the writing rules of the language, such as a missing bracket, so the program cannot runquotation marks · the pair of marks that show where a piece of text starts and endsindentation · spaces at the start of a line that show which lines are inside a loop or an iferror message · a message from the computer that says what is wrong and on which line
Explanation

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.

Python
1name = "Ravi"
2print("Hello " + name)
Output
Hello Ravi
Worked examples
LineMistakeFixed line
prnt("Hi")print is misspeltprint("Hi")
print("Hi)the closing quotation mark is missingprint("Hi")
print "Hi"the brackets are missingprint("Hi")
Each of these three lines has one mistake that stops the program. The computer would not run them until they are fixed.
Python
1for i in range(3):
2print("Go!")
Output
IndentationError: expected an indented block
Indentation shows what is inside the loop. Here the print line is not indented, so the computer stops and shows an error message. Putting four spaces before print fixes it, and then the program prints Go! three times.
MistakeIn a text-based languageIn a block-based language
misspelt commandan error: the word is unknownnot possible, we pick the block
missing bracketsyntax errornot possible, the slots are fixed
wrong order of stepsbug that is hard to seebug, but the order is easy to see
Some mistakes are possible in typed code but impossible in blocks. This is why blocks are good for beginners.
Class activity

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.

Practice
1
Find the two errors and write the corrected lines.
Python
1Print("Good morning")
2print("Good night"
2
Why can a missing bracket not happen in Scratch?
3
What does this program print? Explain the role of the indentation.
Python
1for i in range(2):
2    print("Hi")
3print("Bye")
4
Write one line in Python that prints the word Lola.