Night 18: Your pet's ID card
Bạn đã hoàn thành đêm này bằng Python hoặc mã giả. Kết quả được tính cho cả hai. Bạn có thể luyện tập lại nếu muốn.
Tonight: your pet's ID card
- Your pet needs an ID card with its name, age and food.
- You will learn one thing: records.
Words for tonight
| Word | Meaning |
|---|---|
| record | one card that keeps several facts together |
| field | one fact on the card, like Age |
| type | the plan: which fields every card has |
Plan the card with TYPE
TYPE…ENDTYPEsays which fields the card has. Each field has a name and a type.- Then
DECLAREa variable of that type: a real card.
TYPE PetRecord
DECLARE Name : STRING
DECLARE Age : INTEGER
DECLARE Food : STRING
ENDTYPE
DECLARE Pet : PetRecord
Pet.Name ← "Kit"
Pet.Age ← 2
Pet.Food ← "fish"
OUTPUT Pet.Name, " eats ", Pet.Food

Use a field with a dot
Pet.Agemeans: theAgefield ofPet. It works like a variable.
TYPE PetRecord
DECLARE Name : STRING
DECLARE Age : INTEGER
ENDTYPE
DECLARE Pet : PetRecord
Pet.Name ← "Kit"
Pet.Age ← 2
Pet.Age ← Pet.Age + 1
OUTPUT Pet.Name, " is now ", Pet.Age
Common mistakes
| Wrong | Right | Why |
|---|---|---|
PetRecord.Name ← "Kit" |
Pet.Name ← "Kit" |
the type is only the plan; store in the variable |
OUTPUT Pet |
OUTPUT Pet.Name |
output one field at a time |
forget ENDTYPE |
ENDTYPE after the fields |
only DECLARE lines go inside the type |
- Now make your pet's ID card!
Độ khó 2/5 · 140 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Define a record type PetRecord with three fields: Name : STRING, Age : INTEGER, Food : STRING. Declare Pet : PetRecord, store Kit, 2 và fish, and output Pet.Name.
Nhấn Chạy để xem kết quả ở đây.
Độ khó 2/5 · 140 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
It is Kit's birthday! Add 1 to Pet.Age. Then output the new age: 3.
Nhấn Chạy để xem kết quả ở đây.
Độ khó 2/5 · 140 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Kit has a new toy. Add a field Toy : STRING to the type, store ball in it, and output Kit plays with ball using the fields.
Nhấn Chạy để xem kết quả ở đây.
Độ khó 3/5 · 200 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Read a field name with INPUT. Use CASE OF to output that field: name, age hoặc food. For anything else, output No such thing.
Nhấn Chạy để xem kết quả ở đây.