Student challenge 4
21st Century
After successfully collecting all the missing artefacts from the first time checkpoint, you arrive at what looks like the 21st century. You recognise some cars parked here and there, just like the ones your history teacher told you about. In this timeline, there seem to be some things which will have no value in the museum. Make sure you don't fill your backpack up with things you don't need. More on this later...
At the end of the last challenge, you had written some code to pick up the nearest artefacts. Your code will have looked something like this. Read the comments carefully to make sure you fully understand the code.
Figure 1
Run this code until you have picked up some artefacts and then continue to Task 1.
Task 1: Examine your backpack
You should already have had a look in your backpack using code like this:
You might have noticed that you can only hold ten artefacts in your backpack and might have seen this message: I have 10 things in my backpack. Uh oh! Your avatar was unable to pick up the artefact. Your backpack is full! 🎒
The challenges in the last worksheet asked you to examine what is in your backpack. Have a look back at the code in Figure 1 to find the nearest artefact (lines 13 - 16 reproduced below).
Line 2 above asks the world_state
for a list of all nearby artefacts and line 5 gets the first one. Notice the syntax: it uses square brackets to get the value at that index position. nearby
is a list, a group of values stored in one variable. You can get the first one by using an index of
We will use similar code with the backpack. Try adding this line of code:
You will get output something like this: I have 10 things in my backpack. The first thing in my backpack is a Artefact(type='phone')
To make the output more friendly, try this instead to output a description of the artefact:
Task 2: Examine all artefacts
Write some code with outputs the descriptions of each of your artefacts, e.g. What artefacts have you found?
The artefact at position 0 is a ????? The artefact at position 1 is a ????? The artefact at position 2 is a ????? The artefact at position 3 is a ?????
Make sure that your code uses a while loop and works no matter how many artefacts are in your backpack.
Task 3: Count your loot
It is going to be useful to know exactly what is in your backpack. You can check what you are holding by comparing the descriptions to some constants, in a similar way to direction.NORTH
etc. that you used in the first challenge.
For example, this code compares the type of the first item in your backpack to a string, "phone"
Write some code that counts how many you have of each artefact. On this level, you can pick up phones, keyboards and coins. The strings you need are: "keyboard"
, "phone"
and "coins"
.
Task 4: Drop artefacts by type
Great! Now you have control of your backpack!
You need to pick up exactly five keyboards and five mobile phones. If you have picked up other artefacts, like coins, you will need to drop them.
You can use this code to find an artefact that you don't want, e.g. to find a coins artefact
This code will give you the index position of the first coin in your backpack. If you don't have any coins, it will return -1.
You can tell use the DropAction to drop the artefact at that index, e.g.
action = DropAction(index)
Make sure you check that the index is not -1 before using it for a DropAction or you will drop the last artefact in your backpack instead which you have wanted to keep!
Hint: Think back to Task 2. How did you know the index of a particular artefact? You need this index to drop the item.
Last updated