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 isCorrect!if it is the number, otherwiseHigher(if the guess is lower than the secret) orLower(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 attemptsif it was guessed (N = attempts used) orNot guessed in N attemptsif not.
42
10 50 45 40 42
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