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
3

The same program twice

Computing 6: Unit 11, pp. 150–161
GoalTranslate a small program between blocks and text and explain how the parts match.
New words
translate · to rewrite a program in another language so that it does the same jobvariable · a named place where a program keeps a valuefor loop · a text-based way to repeat lines a set number of timesoutput · what a program shows or prints
Explanation

A program can be written in blocks or in text, and it does the same job. To translate a program, match the parts one by one. A set block that makes a variable matches a line such as total = 0. A repeat block matches a for line. A change block matches a line that works out the new value, such as total = total + 5. A say block matches a print line. In Python, range(3) gives 3 rounds. The lines that belong inside a loop or an if are indented. After translating, trace the new program to check that it gives the same result. A robot or a sensor board can be programmed in either kind of language.

whenclickedsettotalto0changetotalby5repeat3saytotalfor2seconds
Worked examples
Python
1total = 0
2for i in range(3):
3    total = total + 5
4print(total)
Output
15
This is the text-based version of the blocks in the picture above. Each line matches one block, and the output is 15.
BlocksText
set [total] to (0)total = 0
repeat (3)for i in range(3):
change [total] by (5) total = total + 5
say (total)print(total)
The parts of the two programs match one by one. Tracing the program gives total = 5, then 10, then 15.
Python
1score = 12
2if score > 10:
3    print("big")
4else:
5    print("small")
Output
big
Selection is translated in the same way. The if line ends with a colon and the lines inside are indented. With score = 12 the program prints big.
Class activity

Translation cards (no computer): write six block phrases on cards and the six matching lines of text on other cards, for example ‘repeat (4)’ and ‘for i in range(4):’, ‘say (n)’ and ‘print(n)’, ‘set [n] to (2)’ and ‘n = 2’. Shuffle them and match the pairs with a friend. Then write a three-line program in blocks and let your friend translate it into text. Trace both versions with the same numbers. If you have a computer or tablet, ask an adult to help you run a very short Python program.

Practice
1
Translate this program into Python and say what it prints.
setnto2setnton*2repeat4saynfor2seconds
2
Write the blocks for this Python program and say what it prints.
Python
1count = 10
2for i in range(3):
3    count = count - 2
4print(count)
3
Translate these blocks into Python and say what the program prints.
settempto18sayhotfor2secondssaycoolfor2secondsiftemp>25thenelse
4
Mia says ‘block languages are not real programming’. What would you say to her?