Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Iterating Through a List | Lists
Python Ninja
course content

Contenu du cours

Python Ninja

Python Ninja

1. Contrôles de Base
2. Contrôles Avancés
3. Fonctions
4. Boucles
5. If-Else Statements
6. Défis
7. Lists

book
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.

123456
# Existing list inventory = ["cat", "dog", "chicken", "monkey"] # Iterate through the inventory for item in inventory: print("Item:", item)
copy

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.

py

ninja.py

copy

Remember that ninja.put_from_inventory() places the last item from the inventory.

Tâche

Swipe to start coding

Solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 7. Chapitre 3
py

ninja.py

toggle bottom row

book
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.

123456
# Existing list inventory = ["cat", "dog", "chicken", "monkey"] # Iterate through the inventory for item in inventory: print("Item:", item)
copy

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.

py

ninja.py

copy

Remember that ninja.put_from_inventory() places the last item from the inventory.

Tâche

Swipe to start coding

Solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 7. Chapitre 3
Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Nous sommes désolés de vous informer que quelque chose s'est mal passé. Qu'est-il arrivé ?
some-alt