Lesson 7 · Inductive Reasoning

Keep Going Until... The Art of Not Knowing When to Stop

What You'll Learn

How to build programs that keep running until something specific happens — and how to draw conclusions from repeated observations, just like a scientist.

Some Things Don't Have a Fixed End

Think about playing a board game. You don't say "we'll play exactly 15 turns." You play until someone wins. That could take 10 minutes or two hours — you don't know in advance.

Or think about practicing free throws. You don't stop after exactly 20 shots. You keep going until you make 5 in a row, or until dinner's ready, or until your arms get tired.

These are situations where you don't know the end point. You just keep going and observe what happens, round after round, until some condition tells you to stop.

This is called inductive reasoning — building understanding through repeated observation. Scientists do it: run an experiment, observe the result, repeat, and eventually draw a conclusion. Today, you're going to do the same thing — by building a game.

Loops That Run Forever (Until You Say Stop)

In Lesson 5, we learned while loops: "keep going while a condition is True." There's a special version of this that's incredibly useful for games and menus:

Python

How while True Works

while True: creates a loop that would run forever — because True is always True.

But inside the loop, we use break to escape when we're ready.

This pattern is perfect when you don't know how many times to repeat. The loop keeps going, and the code inside decides when to stop.

Think of it as: "Keep doing this. I'll tell you when I'm done."

Rock Paper Scissors — The Logic

Let's build Rock Paper Scissors. Before we code, let's think about the rules. This is deductive reasoning (from Lesson 3) — clear rules that determine the outcome:

The Rules

Rock beats Scissors (rock crushes scissors)

Scissors beats Paper (scissors cut paper)

Paper beats Rock (paper covers rock)

If both choose the same thing → it's a tie

Let's start simple — one round against the computer:

Python · One Round

Play it a few times. Notice that the computer's choice is truly random — sometimes you win, sometimes you lose, sometimes it's a tie. There's no way to predict a single round.

But what if you played 20 rounds? Would you start to notice patterns? That's inductive reasoning — building understanding from repeated observations.

Making It a Real Game

One round isn't a game — it's a coin flip. A real game needs multiple rounds and a way to keep score. Let's combine while True with our RPS logic:

Python · Multi-Round Game

New Things

continue skips the rest of the loop and starts the next round immediately. We use it when the player types something invalid — no point in comparing "banana" to the computer's choice.

not in is the opposite of in: player not in choices means "the player's input isn't in our list of valid choices."

\ at the end of a line means "this line continues on the next line." It lets us split a long condition across multiple lines so it's easier to read.

Play 10+ rounds and look at your stats. Are they roughly even? With random choices, you'd expect to win about 1/3 of the time, lose 1/3, and tie 1/3. Do your results match that prediction? That's inductive reasoning — checking if your observations match a hypothesis.

First to 3 Wins

Instead of playing forever, let's make it a real tournament — first player to win 3 rounds takes the match:

Python · Tournament Mode

The Loop Condition

while player_wins < wins_needed and computer_wins < wins_needed:

This loop keeps going as long as neither player has reached 3 wins. The moment either one hits 3, the condition becomes False and the loop stops.

Notice: ties don't count toward anyone's score. The game could theoretically go on for a very long time if there are many ties — and you can't predict how many rounds it'll take. That's why this is an inductive problem, not a deductive one.

Tracking Patterns in Results

Scientists don't just observe — they look for patterns in their observations. Let's add streak tracking to see if there are any "hot streaks" or "cold streaks":

Python · Streak Tracker

Tracking With Lists

We use results.append("W") to record each round's outcome. At the end, we have a complete history like ["W", "L", "T", "W", "W", "L"].

" ".join(results) turns that list into a readable string: "W L T W W L". The .join() command puts a space between each item.

The streak tracker works by counting consecutive wins. Every win increases current_streak. Any loss or tie resets it to 0. We track the best_streak by checking if the current streak beats the record.

Drawing Conclusions From Data

Let's run a bigger experiment. Instead of playing manually, let's have the computer play against itself 1000 times and see what patterns emerge:

Python · 1000-Game Experiment

Inductive Reasoning in Action

We didn't prove that each player wins 33% of the time using math (that would be deductive reasoning). Instead, we observed 1000 games and noticed the pattern.

This is how induction works: observe, observe, observe, then conclude. The more observations, the more confident the conclusion.

Run it again — you'll get slightly different numbers each time, but they'll always hover around 33/33/33. That consistency across many trials is what makes the conclusion strong.

What If the Computer Cheats?

What if the computer's choices aren't perfectly random? What if it slightly favors rock? Could you detect that through observation?

Python · Detecting Bias

This is a perfect example of inductive reasoning. You didn't know the computer was biased. But by observing many games, you detected the pattern. And once you know the pattern, you can exploit it — always pick paper to beat the computer's favorite move.

This is how scientists work: observe, detect patterns, form a theory, test it.

The Complete Tournament

Let's build the final version — a polished tournament with everything we've learned:

Python · Complete Tournament

New Thing · Dictionaries Preview

emojis = {"rock": "🪨", "paper": "📄", "scissors": "✂️"} is a dictionary — it pairs keys with values. emojis["rock"] gives "🪨".

We also use player_choices as a dictionary to count how often you pick each move.

Dictionaries are like lists, but instead of positions (0, 1, 2) they use names ("rock", "paper", "scissors"). We'll explore them properly in a later lesson.

What You Just Did

You built a game where the outcome is uncertain — you can't predict who will win any single round. But by playing many rounds, you can observe patterns (win rate, streaks, choice bias) and draw conclusions.

That's inductive reasoning.

Unlike deductive reasoning (Lesson 3), where you start with rules and reach guaranteed conclusions, inductive reasoning starts with observations and builds probable conclusions. The more observations, the stronger the conclusion — but it's never 100% certain.

The Big Idea

Some problems can't be solved by thinking ahead — you have to try, observe, and learn. Keep going until you have enough data to see the pattern. This is how scientists discover laws of nature, how athletes find winning strategies, and how you figure out what works and what doesn't.

Thinking Questions

  1. What's the difference between deductive reasoning ("if the rule is X, then Y must happen") and inductive reasoning ("I've observed X twenty times, so Y probably happens")?
  2. After playing many rounds, did you notice yourself favoring a particular choice? Why do humans have trouble being truly random?
  3. If the computer secretly favored scissors 60% of the time, what strategy would you use? How many rounds would you need to detect the bias?

Level Up

Challenge 1 · Number Guessing Game

The computer picks a random number between 1 and 100. The player guesses, and the computer says "too high" or "too low." Keep going until they guess correctly. Track how many guesses it took. Hint: random.randint(1, 100) picks a random whole number.

Challenge 2 · Best of 5 With Stats

Modify the tournament so that after each match, the player can choose to play again. Track stats across all matches (total wins, total losses, matches won). Display an all-time record at the end.

Challenge 3 · Smart Computer

Make the computer track what the player picks most often, then choose the counter. If the player picks rock 50% of the time, the computer should start picking paper more. The computer learns from observation — it does inductive reasoning too!

What You Learned

Thinking Skill

Inductive Reasoning — drawing conclusions from repeated observations. Ask: "What pattern do I see after many trials? What can I conclude?"

Python Concepts

while True: ... break — a loop that runs until you tell it to stop

continue — skip to the next loop iteration

not in — check if something is NOT in a list

.join() — turn a list into a string with a separator

Game loops — the while True pattern for interactive programs

Streak tracking — counting consecutive events

Dictionaries preview{"key": "value"} pairs

Win rate / statistics — calculating percentages from counts

Lessons 1–7

Decomposition — break big problems into small steps

Abstraction — decide what details matter

Deductive Reasoning — rules + facts = guaranteed conclusions

Categorization — organize related items into groups

Pattern Recognition — spot what repeats and what changes

Automation — templates that handle repetitive tasks

Inductive Reasoning — observations → probable conclusions