Sectionย 7. Chapterย 2
knight.py
Changing value by index
Swipe to show menu
In this chapter, we will learn how to change the value of a list element using its index in Python. To modify a specific element, access the list at the desired index and assign it a new value. For example:
123456789# Existing list inventory = ["cat", "dog", "chicken", "monkey"] # Change the second element inventory[1] = "parrot" # inventory is now ["cat", "parrot", "chicken", "monkey"] print(inventory)
You can also change an item in the knight's inventory by index. To do this, you can pass the index to the knight.pick_to_inventory(index) method.
knight.py
Task
Swipe to start coding
Solution
Everything was clear?
Thanks for your feedback!
Sectionย 7. Chapterย 2
knight.py
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat