Python by Projects › Number Detective

Exercises: Number Detective

Build small number gadgets. Then remix the full mystery game.

Keep a paper tally when you want to compare how many guesses you needed. Do not make the program count guesses.

Exercise 1

Build a six-sided dice roller.

Import random. Pick an integer from 1 through 6 with random.randint(). Store it in a clearly named variable and print it in a playful f-string message. Run the cell several times.

How this is checked: You check this one yourself, against the list below.

The site cannot check this one. Run your program, then tick each item below when your work does it.

Check your work

For parents and teachers: report a problem

Exercise 2

Write a higher-or-lower verdict as straight, top-to-bottom code.

Use secret = 37. Ask for one guess and convert it to an integer. Use if, elif, and else to print exactly one verdict: too high, too low, or correct.

How this is checked: You check this one yourself, against the list below.

The site cannot check this one. Run your program, then tick each item below when your work does it.

Check your work

For parents and teachers: report a problem

Exercise 3

(Player B looks away while Player A types the secret!)

Make a two-player take-turns version.

Player A types a secret integer from 1 through 100. Player B guesses until the number is correct. Then swap roles and run the program again. Give high and low hints. Keep guess tallies on paper only.

How this is checked: You check this one yourself, against the list below.

The site cannot check this one. Run your program, then tick each item below when your work does it.

Check your work

For parents and teachers: report a problem

Exercise 4

This code is broken on purpose. Run it and type 12. Read the traceback from the bottom. Find the line Python points to and explain the type problem in a comment. Then fix the code so it prints a verdict.

How this is checked: You check this one yourself, against the list below.

The site cannot check this one. Run your program, then tick each item below when your work does it.

Check your work

For parents and teachers: report a problem

Exercise 5

Remix the loop game as a buried-treasure search.

The computer picks a secret depth from 1 through 50. The player keeps guessing until the depth is correct. A guess above the secret is too deep. A guess below it is too shallow. Write your own victory message.

How this is checked: You check this one yourself, against the list below.

The site cannot check this one. Run your program, then tick each item below when your work does it.

Check your work

For parents and teachers: report a problem

Exercise 6

Build a range-width report.

Start with low = 12 and high = 47. Calculate the span with high - low and the midpoint with (low + high) // 2. Use % to decide whether the span is even or odd. Print the span, midpoint, and even-or-odd result.

Classify the span three ways with if, elif, and else: below 10 is narrow, below 30 is medium, and anything else is wide. Include the line signal = low + high * 2, predict its value before you run it, and print it. Python does the multiplication before the addition.

For one printed message, join text to str(span) with + instead of using an f-string.

Real program

The real program reads low and high with int(input(...)).

How this is checked: You check this one yourself, against the list below.

The site cannot check this one. Run your program, then tick each item below when your work does it.

Check your work

For parents and teachers: report a problem

Exercise 7

Make a one-guess truth check.

Start with secret = 42 and guess = 37. Store the result of guess == secret in a variable named is_correct. Before printing a word verdict, print the Boolean check directly with print(guess == secret). Then use is_correct in an if/else statement to print either Correct! or Keep detecting!.

Real program

The real program reads secret and guess with int(input(...)).

How this is checked: You check this one yourself, against the list below.

The site cannot check this one. Run your program, then tick each item below when your work does it.

Check your work

For parents and teachers: report a problem

Algorithm Extension

Optional enrichment: the sentinel-loop pattern — repeat until a stop signal.

Exercise 8

Remix the full Number Detective game so the secret can be any integer from 1 through 1,000. Update the prompts so the player knows the new range. Keep the loop going until the answer is correct.

After you play, write a comment in your code: How did the larger range feel? Which first guess helped you shrink the mystery quickly?

How this is checked: You check this one yourself, against the list below.

The site cannot check this one. Run your program, then tick each item below when your work does it.

Check your work

For parents and teachers: report a problem

Challenge

Challenge 1

  • Challenge

Add hot and cold hints to Number Detective.

Use comparisons and subtraction to find the distance between a wrong guess and the secret. If the guess is below the secret, subtract the guess from the secret. Otherwise, subtract the secret from the guess. A distance below 6 is hot. Any other distance is cold.

Use only arithmetic and comparisons for the distance clue. Keep any guess tally on paper.

How this is checked: You check this one yourself, against the list below.

The site cannot check this one. Run your program, then tick each item below when your work does it.

Check your work

For parents and teachers: report a problem

Challenge 2

  • Challenge

Make the computer guess your number.

Choose an integer from 1 through 100 on paper. The computer starts by guessing the middle of the possible range. You type 1, 2, or 3. After 1 or 2, change the matching edge of the possible range. Then use (low_edge + high_edge) // 2 for the next middle guess. Repeat until you type 3.

Watch how the possible range gets about half as wide after each hint. Do not make the computer count guesses.

How the replies work: after each computer guess, YOU type a number — 1 means "your guess is too high", 2 means "your guess is too low", 3 means "correct!". Numbers only, so your program can compare them.

How this is checked: You check this one yourself, against the list below.

The site cannot check this one. Run your program, then tick each item below when your work does it.

Check your work

For parents and teachers: report a problem

Back to Number Detective