Lesson 5 · Pattern Recognition

Pattern Detection: The Superpower You Didn't Know You Had

What You'll Learn

How to spot patterns — what stays the same and what changes — and how to use Python to predict the future by following a pattern forward.

You're Already a Pattern Machine

Your brain is constantly spotting patterns without you even noticing. You know that Monday comes after Sunday. You know that when your teacher says "OK, class…" in that tone, a test is coming.

Pattern recognition is noticing what repeats, what stays the same, and what changes. It's how you learned to read (letters make sounds, sounds make words). It's how you learned to catch a ball (it goes up, then it comes down). It's how you predict what's going to happen next.

Today we're going to use patterns to answer a question every kid has asked: "How long until I can afford that thing I want?" You know, things like 'Beats' headphones, a new PlayStation video game, or a new bicycle.

The Saving Problem

Imagine you want to buy a new game that costs $60. You get $10 per week in allowance. How many weeks until you can afford it?

You could figure this out in your head: 10, 20, 30, 40, 50, 60 — six weeks. But what if the numbers aren't so neat? What if you get $7.50 per week and the game costs $54.99? What if you already have some money saved?

That's where code comes in. Let's build a program that follows the pattern forward, week by week, until you reach your goal.

Spotting the Pattern

Before we write any code, let's look at the pattern. If you start with $0 and save $10 per week:

Week Saved
0 $0
1 $10
2 $20
3 $30
4 $40

See the pattern? Two things are happening each week:

The Pattern

What stays the same: You add $10 every week. The process is identical each time.

What changes: The total amount saved. It grows by $10 each time.

This is what pattern recognition is: asking "What repeats? What stays the same? What changes?" Once you spot the pattern, you can follow it forward as far as you want.

Let's make Python follow this pattern with a for loop:

Python

New Thing · range(1, 7)

In Lesson 4, we used range(n) which counts from 0. But range(1, 7) counts from 1 to 6 — it starts at the first number and stops before the second.

So range(1, 7) gives us: 1, 2, 3, 4, 5, 6. Perfect for week numbers.

When You Don't Know How Many Times to Repeat

There's a problem with the code above: we told it to run for exactly 6 weeks. But what if we don't know how many weeks it'll take? What if the goal is $85 and the allowance is $12? You'd have to do the math first to figure out how many weeks — which defeats the purpose of having a computer do it for you.

What we really want to say is: "Keep going until the savings reach the goal." Python has a special loop for this: the while loop.

Python · While Loop

How while Works

A for loop runs a specific number of times: "do this 6 times."

A while loop keeps running as long as a condition is True: "keep going while savings are less than the goal."

Each time through the loop, Python checks the condition again. If it's still True, it runs the code inside again. If it's False, it stops and moves on.

This is perfect for patterns where you know the rule (add allowance each week) but not the end point (how many weeks).

Try changing the goal to 150 or the allowance to 7. The program figures out the right number of weeks automatically because it follows the pattern until it reaches the answer.

The Infinite Loop Trap

There's one important thing to watch out with while loops. What if the condition never becomes False?

Python · Don't Actually Run This!

The Lesson

If saved never increases, it's always less than goal, so the loop never stops. This is called an infinite loop.

The fix is simple: make sure something inside your loop is moving toward the end condition. In our savings tracker, saved = saved + allowance makes saved grow, which eventually makes saved < goal become False.

break is an emergency exit — it forces the loop to stop immediately. We used it here as a safety net.

Visualizing the Pattern

Numbers are good, but pictures are better. Let's use the string multiplication trick from Lesson 2 to make a visual progress bar each week:

Python · Visual Savings Tracker

How the Progress Bar Works

saved / goal gives us how far along we are as a decimal (0.5 = halfway).

Multiply by 100 to get a percentage. Multiply by 20 to get the bar length (out of 20 blocks).

int() rounds down to a whole number — you can't have half a block.

This is actually pattern recognition applied to display — we noticed that the progress fraction can be scaled to any visual size.

Patterns With Surprises

Real life isn't as neat as "$10 every week." What about birthday money? What if you spend some? Let's make our tracker handle a more realistic pattern:

Python · Savings With Extras

The birthday money at week 4 breaks the simple pattern — but the overall pattern (savings grow each week) stays the same. This is an important insight: real patterns usually have exceptions. The skill is recognizing the overall pattern despite the noise.

Spotting Number Patterns

Pattern recognition isn't just about savings. Let's practice spotting patterns in numbers — this is the same skill that helps in math, science, music, and everywhere else:

Python · Number Patterns

New Thing · end=" "

Normally, print() adds a new line after each output. Adding end=" " tells Python to put a space instead, so everything prints on the same line.

Can you figure out the mystery pattern? Run it and look at the numbers. What's the rule? Hint: look at the gaps between the numbers — what pattern do you see there?

The mystery pattern gives 1, 2, 4, 7, 11, 16, 22, 29. The gaps are 1, 2, 3, 4, 5, 6, 7 — each gap grows by 1. Patterns can be about the change itself, not just the numbers.

The Complete Money Goal Tracker

Let's put everything together into a polished program that tracks savings toward any goal:

Python · Complete Money Goal Tracker

New Thing · .rjust()

str(week).rjust(2) right-aligns the number in a space of 2 characters. So week 1 becomes " 1" and week 10 stays "10". This makes the columns line up neatly.

It's a small thing, but it makes the output much easier to read — and noticing that the columns were misaligned is itself pattern recognition!

What You Just Did

You spotted a pattern (savings grow by a fixed amount each week), expressed it in code (a while loop that adds allowance each time), and followed it forward to predict the future (how many weeks until you reach the goal).

That's pattern recognition.

This is how scientists predict weather (patterns in temperature and pressure), how musicians create songs (patterns in rhythm and melody), and how athletes improve (patterns in their performance data). Spotting patterns lets you understand the present and predict the future.

The Big Idea

When you see something happening repeatedly, ask: "What stays the same? What changes? And if I follow this pattern forward, what happens next?" That's how you turn observation into prediction — and prediction into power.

Thinking Questions

  1. What's the difference between a for loop and a while loop? When would you use each one?
  2. Can you think of a pattern in your daily life that you could track with a program?
  3. Why is it important to check that a while loop will eventually stop?

Level Up

Challenge 1 · Countdown Timer

Ask the user for a starting number, then count down to 0 using a while loop. At 0, print "BLASTOFF! 🚀". Bonus: print a warning when it reaches 3.

Challenge 2 · Savings With Spending

Modify the money tracker so the user can also enter a weekly spending amount (like $3 on snacks). Subtract that each week. Does it take longer to reach the goal? How much longer?

Challenge 3 · Growth Pattern

Start with 1 grain of rice. Each day, the amount doubles. Use a while loop to find out how many days until you have over 1,000,000 grains. Display each day's total. The answer might surprise you!

What You Learned

Thinking Skill

Pattern Recognition — spotting what repeats, what stays the same, and what changes. Ask: "What's the pattern here, and where does it lead?"

Python Concepts

while loops — repeat as long as a condition is True

for vs while — use for when you know the count, while when you know the condition

range(start, stop) — count from start to stop-1

Infinite loops — what happens when the condition never becomes False

break — emergency exit from a loop

end=" " — print on the same line

.rjust(n) — right-align text in a space of n characters

Accumulator pattern — building up a total inside a loop: saved = saved + allowance

Lessons 1–5

Decomposition — break big problems into small steps

Abstraction — decide what details matter

Deductive Reasoning — design rules, gather facts, reach conclusions

Categorization — organize related items into meaningful groups

Pattern Recognition — spot what repeats, what stays the same, what changes