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: 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 |
English
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

English
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
English
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!
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
Define a record type PetRecord with three fields: Name : STRING, Age : INTEGER, Food : STRING. Declare Pet : PetRecord, store Kit, 2 and fish, and output Pet.Name.
点击“运行”查看此处输出。
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
It is Kit's birthday! Add 1 to Pet.Age. Then output the new age: 3.
点击“运行”查看此处输出。
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
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.
点击“运行”查看此处输出。
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
Read a field name with INPUT. Use CASE OF to output that field: name, age 或 food. For anything else, output No such thing.
点击“运行”查看此处输出。