🎯 What you will learn
- Combine functions, loops and checks
- Track resources as you build
- Stop when you run out of materials
👋 Let’s get started!
It is time to build a village! Your builder has a limited amount of wood. Each house costs 4 wood. Use a loop to keep building houses while you still have enough wood, and print a message when you run out. 🏘️
🪜 Step by step
1. 🪵 Resources
Start with a number of wood blocks in a variable.
wood = 10
print("Wood:", wood)
2. 🏠 Build houses
A while loop builds houses as long as you have 4 wood or more.
while wood >= 4:
print("House built")
wood = wood - 4
3. 📣 Report
When you cannot build any more, tell the player what is left.
print("Wood left:", wood)
🧪 Try it!
Change the code and press ▶ Run to see what happens. You can’t break anything!
Village builder
wood = 10
houses = 0
while wood >= 4:
houses = houses + 1
wood = wood - 4
print("House", houses, "built")
print("Houses built:", houses)
print("Wood left:", wood)🎨 Unplugged activity
🏘️ Plan your own village on paper with a wood budget. Decide how much each house costs, then count how many you can build. Check your answer with the code!
Plan the village
wood = 13
houses = 0
while wood >= 4:
houses = houses + 1
wood = wood - 4
print("Houses:", houses)📖 Key words
Tap a card to flip it over!
- Resource
- Something you need to build 🪵
- Variable
- A box that stores a value 📦
- while
- Repeat while the rule is true 🔁
🏆 Challenge
You have 9 wood and each house costs 3. Build houses while you can, then print how many houses you built. Press ▶ Run.
Challenge
wood = 9
houses = 0
Knowledge Check Quiz
- What stops the village loop?
- How much wood is left after 2 houses from 10?
- Why track wood in a variable?