Learn Python in 30 Days — Day 25: Program Structure & Planning
Day 25: Program Structure & Planning
Welcome to Day 25 of the Learn Python in 30 Days series!
Today’s lesson marks a huge mindset shift: learning how to break big problems into small functions, organise your code, and think like a programmer.
You’ve already built lots of scripts from loops to user input, file reading, JSON, and even simple classes. Now we take all of that and learn how to structure a full program so it stays readable, testable, expandable, and fun to work on.
Instead of dry examples, today we’ll use three mini projects, a battle simulator, a magic 8-ball, and a tiny text adventure to show how planning and structure make everything easier.
All example files for this series are available on my GitHub: Learn-Python-in-30-Days
Why Structure Matters
As soon as you write anything more than a few lines of code, everything becomes easier if you:
-
give each part of your program a single job
-
move repeated logic into helper functions
-
keep your main loop clean and readable
-
separate input, logic, and output
You’re not just writing Python, you’re organising ideas, behaviour, and data.
By breaking things into smaller pieces, you:
- avoid repeated code
- fix bugs faster
- write more readable programs
- reuse components later
- prepare for bigger projects
Let’s see this in action with fun examples.
Example 1 – Tiny Monster Battle Simulator
We start with something most beginners write as a single messy block, a random damage battle.
Whilst this works, but imagine adding spells, special moves, or different monsters.
It instantly becomes unmanageable.
Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.
Why this structure is better
1. Each function has ONE job
-
roll_damage()→ handles randomness -
player_turn()→ updates monster HP -
monster_turn()→ updates player HP -
print_status()→ handles formatting/output -
main()→ controls the game flow
If something breaks, you instantly know which function to fix.
2. Your main loop is readable
Instead of a wall of code, you get:
This reads like a story.
3. Easy to extend
Want spells, crit hits, or healing potions?
Add new functions without touching the rest.
Example 2 – Magic 8-Ball with Good Structure
Perfect for showing how even simple programs benefit from functions.
First the unstructured version: -
Fast, fun, but very limited and hard to extend.
Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.
Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.
Why this is better
-
Input:
get_question() -
Logic:
get_random_answer() -
Output:
print_answer() -
Flow control:
main()
Perfect demonstration of the “program layers” concept.
-
A silly mode?
-
A “rude answers” mode?
-
A “wise Yoda answers” mode?
Just modify one function.
Example 3 – Tiny Text Adventure Room Navigator
This is a brilliant stepping stone toward the final project on Days 28–30.
This shows:
-
data-driven design
-
helper functions
-
keeping main loop clean
-
how to grow complexity safely
Hardcoded rooms = hardcoded pain.
Next well look at the structured, scalable version.
This is data, not code, improvement would be storing this in a JSON file and removing it from the code all together.
Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.
Why this works so well
- The data drives the game, you can add 20 rooms by editing one dictionary — no new
if/elifchains. - The logic is reusable
move()doesn’t care about the room design — it only uses the data. - The main loop is 3 lines, nice, simple and neat.
Next Up — Day 26 – Testing & Debugging
Tomorrow, you’ll look print() debugging, tracing logic, catching errors cleanly.
All example files for this series are available on my GitHub: Learn-Python-in-30-Days
You can see the full series here Learn Python in 30 Days series!
All example files for this series are available on my GitHub: Learn-Python-in-30-Days
You can see the full series here Learn Python in 30 Days series!





Comments
Post a Comment