I am making a text-based adventure game and am having a little trouble.
I want to delete an item from an inventory list however, it's index is unknown as the user may of picked up other items before the one i'm trying to delete.
How would I go about deleting it?
the list is:
inventory = ["sword", "healing potion"]
and when you go through the game you pick up items and it gets added to this list.
您可以简单地使用remove
方法:
inventory.remove('sword')
Above answer is the best one. Another way to do it is(maybe this could be useful too):
index = inventory.index("sword") #in this way you get the index del(inventory[index]) #and now remove it
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.