forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added solution and test files for Guess the Number Game
- Loading branch information
1 parent
bd98352
commit 7a5124e
Showing
2 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
""" | ||
A module for a Guess the Number game. | ||
Module contents: | ||
- generate_random_number: Generates a random number within a range. | ||
- guess_the_number: Compares a guessed number with the target and returns | ||
feedback. | ||
Created on 8-01-25 | ||
@author: Ameen Agha | ||
""" | ||
|
||
import random | ||
|
||
def generate_random_number(start: int, end: int) -> int: | ||
""" | ||
Generates a random number within a given range (inclusive). | ||
Parameters: | ||
start (int): Start of the range (inclusive). | ||
end (int): End of the range (inclusive). | ||
Returns: | ||
int: A randomly generated number within the range. | ||
Raises: | ||
AssertionError: If start or end are not integers, or start >= end. | ||
Examples: | ||
>>> random.seed(1) # Fixing seed for test reproducibility | ||
>>> generate_random_number(1, 10) | ||
3 | ||
>>> generate_random_number(20, 30) | ||
24 | ||
""" | ||
assert isinstance(start, int) and isinstance(end, int), "Inputs must be integers." | ||
assert start < end, "Start must be less than end." | ||
return random.randint(start, end) | ||
|
||
def guess_the_number(target: int, guess: int) -> str: | ||
""" | ||
Compares a guess with the target number and provides feedback. | ||
Parameters: | ||
target (int): The target number to guess. | ||
guess (int): The player's guessed number. | ||
Returns: | ||
str: Feedback indicating if the guess is too low, too high, or correct. | ||
Raises: | ||
AssertionError: If target or guess are not integers. | ||
Examples: | ||
>>> guess_the_number(10, 5) | ||
'Too low!' | ||
>>> guess_the_number(10, 15) | ||
'Too high!' | ||
>>> guess_the_number(10, 10) | ||
'Correct!' | ||
""" | ||
assert isinstance(target, int) and isinstance(guess, int), "Inputs must be integers." | ||
|
||
if guess < target: | ||
return "Too low!" | ||
elif guess > target: | ||
return "Too high!" | ||
else: | ||
return "Correct!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
""" | ||
Unit test module for the Guess the Number game. | ||
Contains tests for the random number generator and guessing logic. | ||
Created on 11-01-25 | ||
@author: Ameen Agha | ||
""" | ||
|
||
import unittest | ||
from Guess_The_Number_Game import generate_random_number, guess_the_number | ||
|
||
class TestGuessTheNumber(unittest.TestCase): | ||
""" | ||
Test cases for the Guess the Number game functions. | ||
These tests ensure random number generation and guessing logic | ||
work as expected. | ||
""" | ||
|
||
def test_random_number_generation(self): | ||
"""Test random number generation within a valid range.""" | ||
random_numbers = [generate_random_number(1, 10) for _ in range(100)] | ||
self.assertTrue(all(1 <= num <= 10 for num in random_numbers)) | ||
|
||
def test_random_number_invalid_range(self): | ||
"""Test random number generation with invalid range.""" | ||
with self.assertRaises(AssertionError): | ||
generate_random_number(10, 1) | ||
|
||
def test_random_number_non_integer_input(self): | ||
"""Test random number generation with non-integer inputs.""" | ||
with self.assertRaises(AssertionError): | ||
generate_random_number(1.5, 10) | ||
|
||
def test_guess_too_low(self): | ||
"""Test guessing too low.""" | ||
feedback = guess_the_number(10, 5) | ||
self.assertEqual(feedback, "Too low!") | ||
|
||
def test_guess_too_high(self): | ||
"""Test guessing too high.""" | ||
feedback = guess_the_number(10, 15) | ||
self.assertEqual(feedback, "Too high!") | ||
|
||
def test_guess_correct(self): | ||
"""Test guessing correctly.""" | ||
feedback = guess_the_number(10, 10) | ||
self.assertEqual(feedback, "Correct!") | ||
|
||
def test_guess_invalid_inputs(self): | ||
"""Test guessing with non-integer inputs.""" | ||
with self.assertRaises(AssertionError): | ||
guess_the_number(10, "five") | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |