Rich makes terminal output look good. Color, bold text, tables, boxes — all the things plain print() cannot do.
This one is deliberately placed right before Group Project 1. Your project is a terminal application, and the difference between a wall of gray text and something with a colored header and a real scoreboard is enormous for about ten minutes of work.
Nothing here changes how your program works. It changes how it looks. Get the program working first, then come back and make it pretty.
Rich does not come with Python — unlike turtle or math, you have to install it:
pip install richIf that fails with a permissions error, the school machines may not let you install packages. Come and tell me rather than fighting it — there is a way around it, and it is not your problem to solve.
Import Rich's print instead of the built-in one and you can put style tags straight in your text. Everything else about print() works exactly as before.
from rich import print
print("[bold red]Danger![/bold red] The dragon noticed you.")print("[green]You found 12 gold.[/green]")print("[bold cyan]===== INVENTORY =====[/bold cyan]")The tags come in pairs, like [green] and [/green] — the one with the slash turns the style back off. Try red, green, yellow, blue, magenta, cyan, and combinations like [bold underline red].
The other way to do it: make a Console and pass a style separately. Useful when the same style gets reused a lot.
from rich.console import Console
console = Console()
console.print("Welcome to the shop!", style="bold magenta")console.print("Sold out", style="dim")This is the one worth learning. Remember the Kg to Lbs table, where lining up the columns by hand was the fiddly part? Rich does that for you.
from rich.console import Consolefrom rich.table import Table
console = Console()
table = Table(title="High Scores")table.add_column("Player")table.add_column("Score", justify="right")
table.add_row("Ana", "1200")table.add_row("Ben", "980")table.add_row("Cal", "870")
console.print(table)Notice add_row takes strings, not numbers — so if your scores are integers, you will need to convert them on the way in. Which is casting, in the opposite direction from Activity 1.8.
from rich.console import Consolefrom rich.panel import Panel
console = Console()
console.print(Panel("You have entered the cave.", title="Chapter 2"))Good for chapter headings in a story game, or for framing a menu.
Do not start a new program for this. Go back to one of your finished code challenges and make it look better:
To submit: the before and after — your original file and the Rich version, so the difference is visible.
And keep it in mind for the group project. A terminal app with a colored header and a clean table reads as finished in a way that plain text does not.