Session 13
Writing basic code directly in Python (forwards, turn, print, repetition)
Objectives
Write code in Python without the support of Blockly
Write simple programs in Python using code for simple movement e.g. my_van.move_forwards()
Use the print() function in Python (not available in Blockly)
Debug Python programs, demonstrating an understanding of the appropriate syntax
Use indents correctly in Python
Use the Repeat loop … for count in range (n):
Resources | Vocabulary |
---|---|
• Interative Whiteboard (IWB) • Levels 92 to 97 in Rapid Router • Resource sheets S11 sheet 2, S13 sheet 1-3 • Levels Guide | • my_van.move_forwards() • my_van.turn_right() • my_van.turn_left() • for … in range ( … ): • print() |
Let's get started
Show Level 92 on the IWB. [fig S13.1]
Your challenge in this session is to become real Python programmers, without the help of Blockly.
Ask the students to express the algorithm in English (e.g. go forward, turn right, turn left, go forward). They could write this on their mini whiteboards.
How do we write this in Python?
Give out the Blockly — Python Matching Code Cards for forwards, right and left found on S11 Resource Sheet 2. Ask the students to match them up.
Use these to help type in the Python code (remember the from van import Van
line of code appears automatically.)
Introduce the print function which is not available in Blockly.
print() will print whatever you put inside the brackets. If you print more than one thing, they must be separated by commas and bits of text (strings) must be put inside quotes ' '.
Try adding this as a final line of code:
print('The van has arrived!')
What happens?
Main activity
In pairs, give the students time to complete Levels 92 to 94.
Encourage them to add their own message at the end, by using the print function.
Mini review
Bring the students together and ask if they have any questions or comments about what they have done.
Now we are going to look at repetition in Python. Can anyone remember how the repeat loop works in Python?
The students should remember much of this from Session 12, for count in range(n).
Hand out Resource Sheet 1, asking the students to join the repeat loop in Blockly with the corresponding code in Python.
What do we know about the repeat loop in Python?
Go through the words and syntax that must stay the same, and the values that can change – ie the number of loops and the name of the ‘counter’.
Show Level 95 on the IWB, and work out the code together: [fig S13.2]
Give the students a chance to complete Levels 95 and 96 independently, with Level 97 as an extension, as this contains a nested repeat loop as a challenge.
Ask them to record one level solution on their Resource Sheet 2.
Share and review
Create a ‘Tips for Debugging Python’ sheet using Resource Sheet 3 as a template. Have learnt about if statements in Python. Discuss the importance of indents, brackets and colons.
Last updated