๐ Section 7: Add a Quick Stats Display Method
๐ Summary (What you will do)
In this section, you will create a clean, readable stats line for each pet. You will:
- Build a
short_stats()method that returns a formatted string - Use a tiny helper (
bar) to draw a simple stat bar - Practice string formatting so numbers line up neatly
This makes the terminal UI easy to scan during gameplay.
โ Checklist (You must complete these)
- Open
pet_manager.py - Find
class Animal - Locate the leveling methods (
_gain_xp,_xp_to_next) - Right after those methods, add the UI helper shown below
- Type the code by hand so you understand how it works
โ No new constants/settings are added in this section, so you do not need to edit the top-of-file constants.
๐ Core Concepts (New learning for this section)
1) Formatting readable output
When a program prints values, the output should be easy to read quickly.
We use formatted strings (f"...") so we can:
- Insert values directly into the text
- Align numbers to the same width
- Combine words, numbers, and symbols in one clean line
2) Tiny helper functions
The bar helper is a small function that turns a number into a visual bar.
It keeps the main return statement readable by hiding the repeated math.
3) Representing โfullโ vs โemptyโ visually
We use two characters:
โfor filled blocksยทfor empty blocks
This creates a quick progress bar for each stat so you can glance at a petโs status without reading every number.
๐ป Code to Write (Type this by hand in pet_manager.py)
Directions:
- Open
pet_manager.py - Find
class Animal - Scroll to the leveling methods (
_gain_xp,_xp_to_next) - Right after those methods, type the following code by hand:
๐ง Code Review & Key Concepts (What important lines do)
The bar helper
bar = lambda v: "โ" * (v // 10) + "ยท" * (10 - v // 10)
This converts a stat into a 10-character bar:
- The number of
โblocks is based on the value - The remaining spaces are filled with
ยทdots
Aligned numbers
f"Hunger:{self._hunger:3d}"
The :3d makes the number always 3 characters wide, so the columns line up.
A single readable line
return (f"{self._name} (Lv {self._level}) | " ...
The method returns one clean line that includes name, level, and three stat bars.
This is what the dashboard will use to show each pet at a glance.
๐งช Test File: s07_test.py
โ Create this file
Create a new file in the same folder as pet_manager.py called:
s07_test.py
๐ป Code to write in s07_test.py
๐ง What this test is doing (and how it works)
- We create
TestAnimalso we can instantiate anAnimal - We set stats to known values so the output is predictable
- We call
short_stats()and print the result - You should see the pet name, level, and three bars in one line
โ Run the test:
python s07_test.py
If the line looks clean and the bars match the stats, your UI helper works.

