From 72c3e65ff6984dc42b2757c111545bbb81b48cc9 Mon Sep 17 00:00:00 2001 From: Emanfalouji Date: Mon, 6 Jan 2025 03:25:17 -0500 Subject: [PATCH 01/11] Adding the number_type challenge ans test file --- solutions/check_number_type.py | 45 ++++++++++++++++++++++ solutions/tests/test_check_number_type.py | 47 +++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 solutions/check_number_type.py create mode 100644 solutions/tests/test_check_number_type.py diff --git a/solutions/check_number_type.py b/solutions/check_number_type.py new file mode 100644 index 000000000..892f6afe3 --- /dev/null +++ b/solutions/check_number_type.py @@ -0,0 +1,45 @@ +""" +This function asks the user to enter a number and checks if the number +is even or odd. It then returns the result as a string. + +Created on 05 01 2025 +@author: Eman Alfalouji. + +""" + + +def check_number_type(user_input: str) -> str: + """ + The function asks the user to enter a number and determines if it is even or odd. + + + Parameters: + user_input (str): str + Raises: + AssertionError: if the argument is not a integer + + Returns: + results will be a text whether "The number is even", "The number is odd" + or "Enter a valid number " + Examples : + >>> check_odd_check("20") + "The number is even" + >>> check_odd_check("11") + "The number is odd" + """ + + assert user_input.strip().isdigit(), "Enter a valid number" + + number = int(user_input.strip()) + if number % 2 == 0: + return "The number is even" + else: + + return "The number is odd" + +if __name__ == "__main__": + user_number = input("Hi! please enter a number: ") + try: + print(check_number_type(user_number)) + except AssertionError as e: + print(e) diff --git a/solutions/tests/test_check_number_type.py b/solutions/tests/test_check_number_type.py new file mode 100644 index 000000000..207e65bda --- /dev/null +++ b/solutions/tests/test_check_number_type.py @@ -0,0 +1,47 @@ +""" +Unit tests for the check_number_type function. + +Created on 05 01 2025 +@author: Eman Alfalouji +""" + +import unittest +from solutions.check_number_type import check_number_type + + +class TestCheckNumberType(unittest.TestCase): + """Tests the check_number_type function.""" + + def test_even_number(self): + """It should identify even numbers.""" + self.assertEqual(check_number_type("22"), "The number is even") + + def test_odd_number(self): + """It should identify odd numbers.""" + self.assertEqual(check_number_type("15"), "The number is odd") + + def test_negative_even_number(self): + """It should identify negative even numbers.""" + self.assertEqual(check_number_type("-4"), "The number is even") + + def test_negative_odd_number(self): + """It should identify negative odd numbers.""" + self.assertEqual(check_number_type("-7"), "The number is odd") + + def test_input_with_whitespace(self): + """It should handle inputs with leading/trailing whitespace.""" + self.assertEqual(check_number_type(" 8 "), "The number is even") + + def test_invalid_input(self): + """It should raise ValueError for invalid inputs.""" + with self.assertRaises(ValueError): + check_number_type("abc") + + def test_empty_input(self): + """It should raise ValueError for empty inputs.""" + with self.assertRaises(ValueError): + check_number_type("") + + +if __name__ == "__main__": + unittest.main() From 70c78fb8ac01e811c3b96b2c367165a720d64f98 Mon Sep 17 00:00:00 2001 From: Emanfalouji Date: Mon, 6 Jan 2025 21:16:47 -0500 Subject: [PATCH 02/11] updated check_number_type --- solutions/check_number_type.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/solutions/check_number_type.py b/solutions/check_number_type.py index 892f6afe3..7a0303738 100644 --- a/solutions/check_number_type.py +++ b/solutions/check_number_type.py @@ -27,16 +27,15 @@ def check_number_type(user_input: str) -> str: >>> check_odd_check("11") "The number is odd" """ - assert user_input.strip().isdigit(), "Enter a valid number" number = int(user_input.strip()) if number % 2 == 0: return "The number is even" else: - return "The number is odd" + if __name__ == "__main__": user_number = input("Hi! please enter a number: ") try: From ff2bdba3f971ac4970e0a7fbf2741c9d4184f077 Mon Sep 17 00:00:00 2001 From: Emanfalouji Date: Mon, 6 Jan 2025 22:51:22 -0500 Subject: [PATCH 03/11] Upadted files --- solutions/check_number_type.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/solutions/check_number_type.py b/solutions/check_number_type.py index 7a0303738..ce6a34fdf 100644 --- a/solutions/check_number_type.py +++ b/solutions/check_number_type.py @@ -10,13 +10,13 @@ def check_number_type(user_input: str) -> str: """ - The function asks the user to enter a number and determines if it is even or odd. + The function asks the user to enter a number and determines if it is type (even or odd.) Parameters: user_input (str): str Raises: - AssertionError: if the argument is not a integer + AssertionError: if the argument is not a integer or empty Returns: results will be a text whether "The number is even", "The number is odd" @@ -27,9 +27,15 @@ def check_number_type(user_input: str) -> str: >>> check_odd_check("11") "The number is odd" """ - assert user_input.strip().isdigit(), "Enter a valid number" - - number = int(user_input.strip()) + user_input = user_input.strip() + # Check if it is empty + if not user_input: + raise ValueError("Input cannot be empty. Enter a valid number.") + # check if it is a number + if not user_input.lstrip('-').isdigit(): + raise ValueError("Enter a valid number") + number = int(user_input) + # Check if the number is even or odd if number % 2 == 0: return "The number is even" else: @@ -37,8 +43,8 @@ def check_number_type(user_input: str) -> str: if __name__ == "__main__": - user_number = input("Hi! please enter a number: ") + user_number = input("Hi! Please enter a number: ") try: print(check_number_type(user_number)) - except AssertionError as e: + except ValueError as e: print(e) From 6e253c735254a4d2fd0c46cf3ecbc27e23822bc8 Mon Sep 17 00:00:00 2001 From: Emanfalouji Date: Mon, 6 Jan 2025 23:53:02 -0500 Subject: [PATCH 04/11] Fix formatting issues --- solutions/check_number_type.py | 8 ++++---- solutions/tests/test_check_number_type.py | 5 +++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/solutions/check_number_type.py b/solutions/check_number_type.py index ce6a34fdf..1db4f97a7 100644 --- a/solutions/check_number_type.py +++ b/solutions/check_number_type.py @@ -22,17 +22,17 @@ def check_number_type(user_input: str) -> str: results will be a text whether "The number is even", "The number is odd" or "Enter a valid number " Examples : - >>> check_odd_check("20") + >>> check_number_type("20") "The number is even" - >>> check_odd_check("11") + >>> check_number_type("11") "The number is odd" """ user_input = user_input.strip() # Check if it is empty if not user_input: raise ValueError("Input cannot be empty. Enter a valid number.") - # check if it is a number - if not user_input.lstrip('-').isdigit(): + # check if it is a number + if not user_input.lstrip("-").isdigit(): raise ValueError("Enter a valid number") number = int(user_input) # Check if the number is even or odd diff --git a/solutions/tests/test_check_number_type.py b/solutions/tests/test_check_number_type.py index 207e65bda..17aca04a3 100644 --- a/solutions/tests/test_check_number_type.py +++ b/solutions/tests/test_check_number_type.py @@ -6,6 +6,7 @@ """ import unittest + from solutions.check_number_type import check_number_type @@ -20,6 +21,10 @@ def test_odd_number(self): """It should identify odd numbers.""" self.assertEqual(check_number_type("15"), "The number is odd") + def test_zero(self): + """It should identify zero as even.""" + self.assertEqual(check_number_type("0"), "The number is even") + def test_negative_even_number(self): """It should identify negative even numbers.""" self.assertEqual(check_number_type("-4"), "The number is even") From de8e75ae82c9a1440ab2e0624fc8d7dc66d3b010 Mon Sep 17 00:00:00 2001 From: Eman Alfalouji <143728110+Emanfalouji@users.noreply.github.com> Date: Tue, 7 Jan 2025 11:39:16 -0800 Subject: [PATCH 05/11] Update tests doc --- solutions/tests/test_check_number_type.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/solutions/tests/test_check_number_type.py b/solutions/tests/test_check_number_type.py index 17aca04a3..658484647 100644 --- a/solutions/tests/test_check_number_type.py +++ b/solutions/tests/test_check_number_type.py @@ -1,6 +1,12 @@ """ Unit tests for the check_number_type function. +Test categories: + - Standard cases: typical lists with different lengths + - Edge cases: empty lists, single elements + - Defensive tests: wrong input types, assertions + + Created on 05 01 2025 @author: Eman Alfalouji """ From c36b65997bf2822c3adc15658a9375d4525a6e19 Mon Sep 17 00:00:00 2001 From: Emanfalouji Date: Fri, 10 Jan 2025 02:10:50 -0500 Subject: [PATCH 06/11] removing the function call --- solutions/check_number_type.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/solutions/check_number_type.py b/solutions/check_number_type.py index 1db4f97a7..2e38f7933 100644 --- a/solutions/check_number_type.py +++ b/solutions/check_number_type.py @@ -40,11 +40,3 @@ def check_number_type(user_input: str) -> str: return "The number is even" else: return "The number is odd" - - -if __name__ == "__main__": - user_number = input("Hi! Please enter a number: ") - try: - print(check_number_type(user_number)) - except ValueError as e: - print(e) From 148b50e127ee5b17c86f0e1b2964696cf196c87b Mon Sep 17 00:00:00 2001 From: Emanfalouji Date: Sat, 11 Jan 2025 20:31:05 -0500 Subject: [PATCH 07/11] EDITIND Doc. --- solutions/check_number_type.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/solutions/check_number_type.py b/solutions/check_number_type.py index 2e38f7933..517c4b593 100644 --- a/solutions/check_number_type.py +++ b/solutions/check_number_type.py @@ -16,16 +16,25 @@ def check_number_type(user_input: str) -> str: Parameters: user_input (str): str Raises: - AssertionError: if the argument is not a integer or empty + ValueError: If the input is empty. + ValueError: If the input is not a valid integer. Returns: results will be a text whether "The number is even", "The number is odd" - or "Enter a valid number " + or raises an appropriate error. Examples : >>> check_number_type("20") "The number is even" >>> check_number_type("11") "The number is odd" + >>> check_number_type("-11") + "The number is odd" + >>> check_number_type("") + "Input cannot be empty. Enter a valid number." + >>> check_number_type("Eman") + "Enter a valid number" + + """ user_input = user_input.strip() # Check if it is empty From 2570acd06c4184b564e9df8ec2a8a2cd01f3fb18 Mon Sep 17 00:00:00 2001 From: Emanfalouji Date: Sat, 11 Jan 2025 22:17:18 -0500 Subject: [PATCH 08/11] editting doc --- solutions/check_number_type.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/solutions/check_number_type.py b/solutions/check_number_type.py index 517c4b593..f51d74482 100644 --- a/solutions/check_number_type.py +++ b/solutions/check_number_type.py @@ -14,7 +14,8 @@ def check_number_type(user_input: str) -> str: Parameters: - user_input (str): str + user_input (str): A string that represents an integer. + Floats or non-integer formats are not allowed. Raises: ValueError: If the input is empty. ValueError: If the input is not a valid integer. From f38906739cd6c524c426fa09a1dee60c7fd4c232 Mon Sep 17 00:00:00 2001 From: Emanfalouji Date: Sat, 11 Jan 2025 22:41:56 -0500 Subject: [PATCH 09/11] editing parameter doc --- solutions/check_number_type.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/solutions/check_number_type.py b/solutions/check_number_type.py index f51d74482..d49583d5a 100644 --- a/solutions/check_number_type.py +++ b/solutions/check_number_type.py @@ -33,7 +33,7 @@ def check_number_type(user_input: str) -> str: >>> check_number_type("") "Input cannot be empty. Enter a valid number." >>> check_number_type("Eman") - "Enter a valid number" + "Please enter a valid number" """ @@ -43,7 +43,7 @@ def check_number_type(user_input: str) -> str: raise ValueError("Input cannot be empty. Enter a valid number.") # check if it is a number if not user_input.lstrip("-").isdigit(): - raise ValueError("Enter a valid number") + raise ValueError("Please enter a valid number") number = int(user_input) # Check if the number is even or odd if number % 2 == 0: From 26f065335c07695fb22cbeb3c0687a6b76d1646c Mon Sep 17 00:00:00 2001 From: Emanfalouji Date: Sat, 11 Jan 2025 23:19:46 -0500 Subject: [PATCH 10/11] final editing --- solutions/check_number_type.py | 13 ++++++++++--- solutions/tests/test_check_number_type.py | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/solutions/check_number_type.py b/solutions/check_number_type.py index d49583d5a..12dd90df7 100644 --- a/solutions/check_number_type.py +++ b/solutions/check_number_type.py @@ -14,7 +14,8 @@ def check_number_type(user_input: str) -> str: Parameters: - user_input (str): A string that represents an integer. + user_input (str): str + A string that represents an integer. Floats or non-integer formats are not allowed. Raises: ValueError: If the input is empty. @@ -31,9 +32,15 @@ def check_number_type(user_input: str) -> str: >>> check_number_type("-11") "The number is odd" >>> check_number_type("") - "Input cannot be empty. Enter a valid number." + Traceback (most recent call last): + ... + .ValueError:"Input cannot be empty. Enter a valid number." >>> check_number_type("Eman") - "Please enter a valid number" + Traceback (most recent call last): + ... + .ValueError:"Please enter a valid number" + + """ diff --git a/solutions/tests/test_check_number_type.py b/solutions/tests/test_check_number_type.py index 658484647..ee39a41a1 100644 --- a/solutions/tests/test_check_number_type.py +++ b/solutions/tests/test_check_number_type.py @@ -3,7 +3,7 @@ Test categories: - Standard cases: typical lists with different lengths - - Edge cases: empty lists, single elements + - Edge cases: empty lists, single element - Defensive tests: wrong input types, assertions From dba5a6168275afa6e5924e72242cc346868e45e5 Mon Sep 17 00:00:00 2001 From: Emanfalouji Date: Sat, 11 Jan 2025 23:27:53 -0500 Subject: [PATCH 11/11] final --- solutions/check_number_type.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/solutions/check_number_type.py b/solutions/check_number_type.py index 12dd90df7..3fe4685dc 100644 --- a/solutions/check_number_type.py +++ b/solutions/check_number_type.py @@ -34,11 +34,11 @@ def check_number_type(user_input: str) -> str: >>> check_number_type("") Traceback (most recent call last): ... - .ValueError:"Input cannot be empty. Enter a valid number." + ValueError:"Input cannot be empty. Enter a valid number." >>> check_number_type("Eman") Traceback (most recent call last): ... - .ValueError:"Please enter a valid number" + ValueError:"Please enter a valid number"