Night 28: Plan your project
Вы уже прошли этот вечер на Python или псевдокоде. Он засчитывается для обоих языков. При желании можете потренироваться ещё раз.
Tonight: plan your project
- In three nights you build your own pet game. Tonight you plan it.
- You will learn one thing: how to plan a program before you write it.
Words for tonight
| Word | Meaning |
|---|---|
| plan | what you will do, written before you start |
| step | one small part of the work |
| test | try the code to see if it is right |
What does the game do?
- Write the answers in plain words first:
| Question | Our pet game |
|---|---|
| What does it remember? | energy (0 to 10) and food (0 to 10) |
| What can the player type? | play, eat, sleep, quit |
| What does it show? | the energy and food after each command |
Break it into small parts
- A big program is many small parts. Each part is one function.
| Command | Function | What it does |
|---|---|---|
| (after each) | status(energy, food) |
the text Energy 5, Food 3 |
play |
play(energy) |
2 energy less, never below 0 |
eat |
eat(food) |
3 food more, never above 10 |
| (warnings) | pet_mood(energy, food) |
hungry, tired or happy |
Build and test one part at a time
- Write one function. Test it with a few
printlines. Then the next one.
def play(energy):
energy = energy - 2
if energy < 0:
energy = 0
return energy
print(play(5))
print(play(1))
- Test the edges: what happens at 0, at 1, at 10?
Tonight's tasks are the parts
- Each task is one part of the plan.
- Tomorrow you join them together with a game loop.
Сложность 2/5 · 140 монет + небольшой бонус за первое завершение сегодня
Part 1 of the plan. Define status(energy, food). It отдачи the text Energy 5, Food 3 for status(5, 3).
Нажмите Запустить, чтобы увидеть результат здесь.
Сложность 2/5 · 140 монет + небольшой бонус за первое завершение сегодня
Part 2. Define play(energy). Playing uses 2 energy, so it returns energy - 2. But energy never goes below 0: play(1) отдачи 0.
Нажмите Запустить, чтобы увидеть результат здесь.
Сложность 2/5 · 140 монет + небольшой бонус за первое завершение сегодня
Part 3. Define eat(food). Eating adds 3 food, but food is never more than 10: eat(9) отдачи 10.
Нажмите Запустить, чтобы увидеть результат здесь.
Сложность 3/5 · 200 монет + небольшой бонус за первое завершение сегодня
Part 4. Define pet_mood(energy, food). It returns "hungry" when food is 0, else "tired" when energy is 0, else "happy".
Нажмите Запустить, чтобы увидеть результат здесь.