Night 18: Your pet's ID card
今夜の課題はPythonまたは疑似コードですでに完了しています。両方の言語で完了として記録されます。よければもう一度練習してください。
このページには最新のブラウザ(SharedArrayBuffer対応)が必要です。Chrome、Edge、Firefox、Safariを最新バージョンに更新してください。
English
Tonight: your pet's ID card
- Your pet needs an ID card with its name, age and food.
- You will learn one thing: dictionaries.
Words for tonight
| Word | Meaning |
|---|---|
| dictionary | a set of keys, each with a value |
| key | the name you look up, like "age" |
| value | what is stored for a key, like 2 |
English
Make a dictionary
- Use
{ }. Writekey: valuepairs, with commas between them.
pet = {"name": "Kit", "age": 2, "food": "fish"}
print(pet["name"])
print(pet["food"])
| Key | Value |
|---|---|
"name" |
"Kit" |
"age" |
2 |
"food" |
"fish" |

English
List or dictionary?
| List | Dictionary |
|---|---|
["Kit", 2, "fish"] |
{"name": "Kit", "age": 2, "food": "fish"} |
find by position: pet[1] |
find by key: pet["age"] |
| good for many things of one kind | good for many facts about one thing |
English
Change and add
- Use the key in
[ ]with=to change a value, or to add a new key.
pet = {"name": "Kit", "age": 2}
pet["age"] = 3
pet["toy"] = "ball"
print(pet)
English
Is the key there?
inchecks if a dictionary has a key.
pet = {"name": "Kit", "age": 2}
if "toy" in pet:
print(pet["toy"])
else:
print("No toy yet")
- Now make your pet's ID card!
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
Make a dictionary pet with three keys: "name" is "Kit", "age" is 2, "food" is "fish". Print the pet's name.
実行ボタンをクリックして出力を確認してください。
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
It is Kit's birthday! Add 1 to pet["age"]. Then print the new age: 3.
実行ボタンをクリックして出力を確認してください。
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
Kit has a new toy. Add the key "toy" with the value "ball". Then print Kit plays with ball, using the dictionary.
実行ボタンをクリックして出力を確認してください。
難易度 3/5 · 200 コイン + 今日初回達成時の小ボーナス
Read a key with input(). If the key is in pet, print its value. If not, print No such thing.
実行ボタンをクリックして出力を確認してください。