Looting
Hoppy found a shiny gem! He needs to put it in the backpack.
You can add items to a list using methods like .append() and .insert().
Adding Spells
backpack = ["Map", "Compass"]
# .append(item) adds to the END
backpack.append("Water")
# Now: ["Map", "Compass", "Water"]
# .insert(index, item) puts it AT a specific spot
backpack.insert(0, "Key")
# Now: ["Key", "Map", "Compass", "Water"]
Your Mission
1
The Bag
Start with loot = ["Gold", "Silver"].
2
Diamond
Use .append() to add "Diamond" to the end of the list.
3
Ruby
Use .insert() to put "Ruby" at the very beginning (index 0).
4
Check
Print the loot list.
Suggested SolutionClick to expandClick to collapse
The list changes as you use these methods.
loot = ["Gold", "Silver"]
loot.append("Diamond")
loot.insert(0, "Ruby")
print(loot)Advanced TipsWant more? Click to expandClick to collapse
You can also combine two lists using the + operator!
more_loot = ["Emerald", "Sapphire"] total = loot + more_loot
pymain.py
Loading...
Terminal
Terminal
Ready to run...