Night 18: Your pet's ID card
Você já concluiu esta noite, em Python ou pseudocódigo. Ela conta para ambos. Pratique novamente se quiser.
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!
Dificuldade 2/5 · Moedas 140 + pequeno bônus na primeira conclusão hoje
Define a record type PetRecord with three fields: Name : STRING, Age : INTEGER, Food : STRING. Declare Pet : PetRecord, store Kit, 2 e fish, and output Pet.Name.
Clique em Executar para ver a saída aqui.
Dificuldade 2/5 · Moedas 140 + pequeno bônus na primeira conclusão hoje
It is Kit's birthday! Add 1 to Pet.Age. Then output the new age: 3.
Clique em Executar para ver a saída aqui.
Dificuldade 2/5 · Moedas 140 + pequeno bônus na primeira conclusão hoje
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.
Clique em Executar para ver a saída aqui.
Dificuldade 3/5 · Moedas 200 + pequeno bônus na primeira conclusão hoje
Read a field name with INPUT. Use CASE OF to output that field: name, age ou food. For anything else, output No such thing.
Clique em Executar para ver a saída aqui.