Projects · 9 / 19 Exercise Medium

Guessing game

Objective

Create a Python program that simulates a number guessing game. To make it testable, the secret number and the guesses are read from the input, and the hints are produced by a generator.

Instructions

  • Read the secret number (first line) and the player guesses separated by spaces (second line).
  • Only the first 7 guesses count. Write a generator hints(secret, guesses) that produces (guess number, hint) pairs.
  • For each guess show Attempt k: hint. The hint is Correct! if it is the number, otherwise Higher (if the guess is lower than the secret) or Lower (if it is higher). Append (close) if the difference is 5 or less.
  • The game ends when the guess is right (later guesses are ignored).
  • At the end show You won in N attempts if it was guessed (N = attempts used) or Not guessed in N attempts if not.
Input
42
10 50 45 40 42
Output
Attempt 1: Higher
Attempt 2: Lower
Attempt 3: Lower (close)
Attempt 4: Higher (close)
Attempt 5: Correct!
You won in 5 attempts

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise