Number guessing game with a loop, decisions and a counter

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Number guessing game with a loop, decisions and a counter

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line makes the computer pick a secret number from 1 to 100?
A secret = random.randint(1, 100)
B secret = random(1, 100)
2. Which loop keeps a guessing game running until the player is right?
A while guess == secret:
B while guess != secret:
3. Where should the counter start before the loop begins?
A guesses = secret
B guesses = 0
4. Which line counts one more guess each turn of the loop?
A guesses = guesses + 1
B guesses == guesses + 1
5. The player guesses 40 and the secret is 25. Which hint is correct?
A Too high
B Too low

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1secret = random.randint(1, 20) Aif guess > secret: print("Too high!")
2while guess != secret: BThe counter is reset to 0 on every turn of the loop, so it can never count up. It should start at 0 before the loop and add 1 inside it.
3Count each turn by adding 1 to the guesses counter inside the loop CA single = assigns a value to a variable. To compare two values you need the double equals == operator.
4Give a hint when the guess is bigger than the secret number DKeeps repeating the indented lines until the player's guess is equal to the secret number
5if guess = secret: print("Correct!") EPicks a random whole number from 1 to 20 and stores it as the secret number the player must guess
6secret = random.randint(1, 20) while guess != secret: guesses = 0 Fguesses = guesses + 1

Definitions

while guess != secret:

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
import random secret = random._______(1, 10)
2.
secret = 7 guess = 0 while guess __ secret:
3.
while guess != secret: guess = int(input("Guess: ")) count __ 1
4.
if guess < secret: print("Too low!") ____ guess > secret:
5.
print("Too high!") ____: print("Correct!")

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A import random
B secret = random(1, 10)
C guess = 0
D while guess != secret:
E guess = int(input("? "))
Question 2
A include random
B secret = random.randint(1, 20)
C guess = int(input("Guess: "))
D print("Your guess:", guess)
Question 3
A count == 0
B secret = 7
C guess = 0
D while guess != secret:
E guess = int(input("? "))
F count = count + 1
Question 4
A while guess != secret:
B guess = int(input("? "))
C if guess > secret
D print("Too high!")
E elif guess < secret:
F print("Too low!")
Question 5
A guess = 0
B while guess = secret:
C guess = int(input("? "))
D if guess == secret:
E print("Correct!")

Answer Key

Number guessing game with a loop, decisions and a counter
Section A: Quick Questions
Q1
A — secret = random.randint(1, 100)
Q2
B — while guess != secret:
Q3
B — guesses = 0
Q4
A — guesses = guesses + 1
Q5
A — Too high
Section B: Match the Pairs
Q1
secret = random.randint(1, 20) → Picks a random whole number from 1 to 20 and stores it as the secret number the player must guess
Q2
while guess != secret: → Keeps repeating the indented lines until the player's guess is equal to the secret number
Q3
Count each turn by adding 1 to the guesses counter inside the loop → guesses = guesses + 1
Q4
Give a hint when the guess is bigger than the secret number → if guess > secret: print("Too high!")
Q5
if guess = secret: print("Correct!") → A single = assigns a value to a variable. To compare two values you need the double equals == operator.
Q6
secret = random.randint(1, 20) while guess != secret: guesses = 0 → The counter is reset to 0 on every turn of the loop, so it can never count up. It should start at 0 before the loop and add 1 inside it.
Section C: Definitions
Q1
while guess != secret: — The loop at the heart of a guessing game. It keeps asking until the guess matches the random secret number. If/elif/else gives hints and a counter adds 1 each turn.
Section D: Fill in the Blanks
Q1
import random secret = random.randint(1, 10)
Q2
secret = 7 guess = 0 while guess != secret:
Q3
while guess != secret: guess = int(input("Guess: ")) count += 1
Q4
if guess < secret: print("Too low!") elif guess > secret:
Q5
print("Too high!") else: print("Correct!")
Section E: Spot the Error
Q1
B — random is the module, not a function. The function that picks a whole number in a range is random.randint(1, 10).
Q2
A — Python uses the import keyword, not include. It should be import random, otherwise random.randint will not work.
Q3
A — == compares values, it does not store one. To start the counter at zero, use a single = : count = 0.
Q4
C — The if line is missing its colon at the end. It should be if guess > secret:
Q5
B — A single = assigns a value, so it can't be used as a loop test. The loop should keep going while the guess is not the secret: while guess != secret: