Contenu du cours
Python Ninja
Python Ninja
Iterating Through a List
Iterating through a list allows you to access and process each item individually. This is useful for performing actions on all items in your inventory.
A for
loop can help you go through each item in the list.
# Existing list inventory = ["cat", "dog", "chicken", "monkey"] # Iterate through the inventory for item in inventory: print("Item:", item)
You can get a list of items in the inventory by writing ninja.inventory.values()
. Now, instead of relying on the position on the map, you can iterate through the ninja's inventory and perform actions on each element in it.
ninja.py
Remember that ninja.put_from_inventory()
places the last item from the inventory.
Swipe to start coding
Solution
Merci pour vos commentaires !
ninja.py
Iterating Through a List
Iterating through a list allows you to access and process each item individually. This is useful for performing actions on all items in your inventory.
A for
loop can help you go through each item in the list.
# Existing list inventory = ["cat", "dog", "chicken", "monkey"] # Iterate through the inventory for item in inventory: print("Item:", item)
You can get a list of items in the inventory by writing ninja.inventory.values()
. Now, instead of relying on the position on the map, you can iterate through the ninja's inventory and perform actions on each element in it.
ninja.py
Remember that ninja.put_from_inventory()
places the last item from the inventory.
Swipe to start coding
Solution
Merci pour vos commentaires !