easy +10 pts

Guess Number Game

Implement a guessing game function with limited attempts and feedback.

You are to write a function `guess_number(secret, guess, attempts_left)`. The function simulates one guess in a number guessing game. - `secret` (int): The number to guess. - `guess` (int): The player's guess. - `attempts_left` (int): The number of attempts left after this guess (must be >= 0). The function should return a string based on the following rules: 1. If `guess` equals `secret`, return `"You guessed it!"`. 2. If `attempts_left` is 0, return `"Game over!"` regardless of the guess (unless the guess is correct, in which case rule 1 applies). 3. If `guess` is less than `secret`, return `"Too low!"`. 4. If `guess` is greater than `secret`, return `"Too high!"`. Write the function with the exact signature `def guess_number(secret: int, guess: int, attempts_left: int) -> str:`. Your implementation should not print anything; it should only return the described string.

Constraints

Inputs are integers. `attempts_left` is non-negative. The function must handle all integer values, including negative values for secret and guess.

Example

>>> guess_number(10, 5, 3)
'Too low!'
>>> guess_number(10, 15, 3)
'Too high!'
>>> guess_number(10, 10, 3)
'You guessed it!'
>>> guess_number(10, 5, 0)
'Game over!'
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Check for correct guess first before checking attempts.
If attempts_left is 0 and guess is not correct, return 'Game over!'.
Use simple if-elif-else statements.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.