Teacher Challenge 4 Guide

Introduction

Objectives

  • To extend algorithmic thinking to tasks to work out appropriate solutions

  • To explore the use of selection in a loop

  • To further explore the use of lists with indexing and iteration

Preparing for the Lesson

Timing and Prior Experience

TimingPupils' prior experience

One lesson of approximately 50 mins

Pupils should be familiar with Python programming, including the use of:

  • Variables

  • Assignment

  • Selection

  • Functions and procedures (subroutines)

  • Parameters

  • Loops

  • Lists

Teacher Preparation

Read over the pupil worksheet

Delivering the lesson

This lesson builds on the previous lessons and allows your pupils to further investigate the use of selection, loops, arrays (lists in Python) as well as iterating over lists and using selection in a loop.

Getting the pupils started

Ask pupils to read the introduction on their worksheet. We have provided them with starter code so they are all ready to move on to the next tasks. Ensure that they read the code carefully, using the comments to help them to fully understand it.

The running code will move their avatar towards the nearest artefacts and pick them up.

Task 1: Examine your backpack

In this task, the code is examined further and pupils are are to extend it to show the description of the first thing in their backpack. They should be familiar with lists already but you might like to point out that the backpack is a list so they can look at individual elements using indexes in square brackets.

Task 2: Examine all artefacts

In the second task, pupils are asked to write a while loop to discover and output descriptions of each element in their backpack.

Pupils need to be able to use len() and list indexing.

Here is the suggested solution. Note that Kurono supports Python's formatted string literals (also called f-strings for short). A second example solution has been provided in case your class is not familiar with these.

Solution using f-strings

my_backpack = avatar_state.backpack
length = len(my_backpack)

print(f"I have {length} things in my backpack.")

index = 0
while index < length:
    print(f"The item at position {index} is a {my_backpack[index].type}")    
    index = index + 1

Solution without f-strings

my_backpack = avatar_state.backpack
length = len(my_backpack)

my_backpack = avatar_state.backpack
length = len(my_backpack)

print("I have", length, "things in my backpack.")

index = 0
while index < length:
    print("The item at position", index, "is a", my_backpack[index].type)    
    index = index + 1

Extension: can pupils change the output message so it outputs "a" or "some" depending on the item?

Here is one solution:

index = 0
while index < length:
    item = my_backpack[index].type # Get the item
    last_char = item[len(item)-1] # Get the last letter
    word = "a" # Set the word we need to "a"
    if last_char == "s":
        word = "" # Clear the word, we don't need it
    print(f"The item at position {index} is {word} {my_backpack[index].type}")    
    index = index + 1

Task 3: Count your loot

In this task, pupils need to count their artefacts by type. They need to create a variable for each artefact type, e.g. phone_count and add 1 to this value each time they find a match within a loop.

Scaffolding

If they are unsure how to proceed, ask them where in their code they know the type of a single artefact. Answer: in the loop, here

index = 0
while index < length:
    # here
    print("The item at position", index, "is a", my_backpack[index].type)    
    index = index + 1

Discuss the concept of running totals and perhaps try this as an exercise with any who are struggling to understand the concept:

I am going to say numbers to you and when I have finished, I want you to tell me their total. You cannot write them down or use a calculator. I might give you a lot of numbers so you won't be able to remember them all when I have finished. How are you going to add them up?

If they do not come up with a running total solution, then actually do this. Give them fairly simple numbers to add together but give them too many for them to be able to remember them all. Then ask them what they did.

What they did was this: every time you gave them a number, they added it to a total in their heads. The next question is: what was the total before I started giving you numbers? Answer: 0

index = 0
phone_total = 0    # create a total
while index < length:
    if my_backpack[index].type == "phone": # If the item is a phone...
        phone_total = phone_total + 1             # add 1 to the total 
        
    print("The item at position", index, "is a", my_backpack[index].type)    
    index = index + 1
print("I have", phone_total, "phones") # Output the total OUTSIDE the loop

Then they need to add code to count up phones and keyboards.

Here is one solution:

index = 0
phone_total = 0 
keyboard_total = 0
coins_total = 0

my_backpack = avatar_state.backpack
length = len(my_backpack)

while index < length:
    item = my_backpack[index].type
    if item == "phone":
        phone_total = phone_total + 1
    elif item == "keyboard":
        keyboard_total = keyboard_total + 1
    elif item == "coins":
        coins_total = coins_total + 1
        
    print("The item at position", index, "is a", my_backpack[index].type)    
    index = index + 1
print("I have", phone_total, "phones")
print("I have", keyboard_total, "keyboards")
print("I have", coins_total, "coins")

Extension: Some pupils might like to use lists for this task. They could create a 1D list of artefact type strings, e.g. TYPES = ["phone", "keyboard", "coins"] and then have a separate counting list, e.g. counts = [0,0,0] They could use a 2D list instead, if they prefer.

Task 4: Drop artefacts by type

In this task, pupils are asked to make sure they have exactly five mobile phones and five keyboards. The backpack only holds ten things so they will need to drop any coins they are holding.

They can use a new function, find to get the index value of the first item of a specific type in their backpack. E.g.

index = my_backpack.find("coins")

Then they can return a DropAction with this index value so that Kurono removes this item from their backpack.

It is important to note that this function returns -1 if they are not holding an item of this type. They must only return a DropAction if the index is NOT -1. Otherwise, the last item in their backpack will be dropped and they might have wanted that!

Scaffold: first get pupils to drop coins. Then ask them to extend their code so that it drops mobile phones or keyboards if they have more than five of them.

Here is one solution.

Step 1: Drop coins

index = my_backpack.find("coins")
if index > -1:
    action = DropAction(index)
else:
    # Check if the current cell holds an artefact
    # ETC
return action

Step 2: Drop phones or keyboards if you have more than five of them

index = my_backpack.find("coins")
if index > -1:
    action = DropAction(index)
elif phone_total > 5:
    index = my_backpack.find("phone")
    action = DropAction(index)
elif phone_total > 5:
    index = my_backpack.find("keyboard")
    action = DropAction(index)        
else:
    # Check if the current cell holds an artefact
    # ETC
return action

Summary

These tasks have allowed pupils to further explore the use of lists, iteration and selection in a loop, as well as interacting with the Kurono API.

Last updated

Logo

Rapid Router resources

KS1KS2KS3

Kurono resources

TeacherStudent

© Copyright 2023 Ocado Group plc. All rights reserved