Open advent6 and File, Save As, advent7
Using lists in your adventure to store and retrieve items
Creating and printing a list
Vocabulary: list A list stores multiple data items such as numbers and strings (text and numbers). Data items can be added and removed from a list. A list can be searched to see if certain items are inside it.
rucksack = ["gold coin","rope","cheese roll"] #create list called rucksack
print(rucksack[0]) #print first item in the list
print(rucksack[2]) #Print third item in list
print(rucksack) #print whole list
This would output as
>>>
gold coin
cheese roll
['gold coin','rope','cheese roll']
>>>
Note 0 refers to the first item in the list, 1 to the second item etc
Create an empty list and add things as you go
rucksack = [ ]
Adding items to the end of a list
rucksack = ["gold coin","rope","cheese roll"] #This creates a list called rucksack
rucksack.append("sword") #This adds the sword onto the list rucksack
print(rucksack) #Print rucksack list
This would output as
>>>
['gold coin','rope','cheese roll','sword']
>>>
Insert item into a specific place in the list
rucksack = ["gold coin","rope","cheese roll"]
rucksack.insert(2,"fried fish") #This will insert fried fish at position 2 in the list
print(rucksack)
This would output as
>>>
['gold coin','rope','fried fish','cheese roll']
>>>
Note 2 is actually third place in the list as first is position 0
Deleting items from a list
rucksack = ["gold coin","rope","cheese roll"]
rucksack.remove("rope") #Removes rope from list
print(rucksack)
This would output as
>>>
['gold coin','cheese roll']
>>>
Checking to see if something is in a list
rucksack = ["gold coin","rope","cheese roll"]
if "cheese roll" in rucksack:
print("You have a cheese roll")
else:
print("You don't have a cheese roll")
This would output as
>>>
You have a cheese roll
>>>
You can also use not in such as
if "cheese roll" not in rucksack:
Combining two lists
playerrucksack = ["gold coin","rope","cheese roll"]
chest1 = ["ruby","emerald","Wand of Doom"]
playerrucksack = playerrucksack +chest1
print(playerrucksack)
This would output as
>>>
['gold coin','rope','cheese roll','ruby','emerald','Wand of Doom']
>>>
Create a list and use it at least twice in your program.