セクション 7. 章 1
ninja.py
リストの紹介
メニューを表示するにはスワイプしてください
Pythonのリストは、数値、単語、オブジェクトなど、さまざまな要素を格納できるコンテナのようなものです。リストは、要素を角括弧[]で囲むことで作成でき、append()を使うことで新しい要素をリストの末尾に簡単に追加できます。
123456789# Create a list with initial items inventory = ["cat", "monkey"] # Add a new item to the end of the list inventory.append("dog") # Print the updated list print("Updated Inventory:", inventory)
また、pop(index)を使うことでリストから要素を削除できます。このメソッドは指定した位置の要素を削除し、その値を返します。インデックスを指定しない場合は、最後の要素が削除されます。
1234567891011121314# Existing list inventory = ["cat", "dog", "chicken", "monkey"] # Get the values last_item = inventory.pop() second_item = inventory.pop(1); # Print the accessed items print("Last item:", last_item) print("Second item:", second_item) # Print the updated list print("Updated Inventory:", inventory)
同様に、忍者のインベントリも実装されており、cat, dog, chicken, monkey, parrot, pig などの値を保持可能。
次のメソッドで操作可能:
pick_to_inventory(index): アイテムを取得し、指定したindexにインベントリへ配置。indexが指定されていない場合は末尾に追加。put_from_inventory(index): インベントリからindexでアイテムを取り出し、マップ上に配置。indexが指定されていない場合は最後のアイテムを取り出す。
リストのインデックスは0から始まります。つまり、最初の要素のインデックスは0、2番目は1、3番目は2というようになります。
次は、忍者がインベントリに2匹の動物を集め、最初の要素をマップに戻す例です。
ninja.py
タスク
スワイプしてコーディングを開始
解答
他のコースを探す カタログ内
すべて明確でしたか?
フィードバックありがとうございます!
セクション 7. 章 1
ninja.py
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください