Modification de la Valeur par Indice
Dans ce chapitre, nous allons apprendre à changer la valeur d'un élément de liste en utilisant son index en Python. Pour modifier un élément spécifique, accédez à la liste à l'index souhaité et attribuez-lui une nouvelle valeur. Par exemple :
# Existing list inventory = ["cat", "dog", "chicken", "monkey"] # Change the second element inventory[1] = "parrot" # inventory is now ["cat", "parrot", "chicken", "monkey"] print(inventory)
Vous pouvez également changer un élément dans l'inventaire du ninja par index. Pour ce faire, vous pouvez passer l'index à la méthode ninja.pick_to_inventory(index)
.
ninja.py
Tâche
Swipe to start coding
Solution
Tout était clair ?
Merci pour vos commentaires !
Section 7. Chapitre 2