Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check_Number_Type #42

Merged
merged 11 commits into from
Jan 11, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions solutions/check_number_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""
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 type (even or odd.)


Parameters:
user_input (str): str
A string that represents an integer.
Floats or non-integer formats are not allowed.
Raises:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each assertion checks for only one assumption about the argument, each argument have one like,
ValueError: .....
ValueError: .....

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 raises an appropriate error.
Examples :

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring includes only two doctests passing , Includes 3 or more passing doctests

>>> 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("")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Detail input assumptions and expand the Raises section to document defensive assertions thoroughly.

Traceback (most recent call last):
...
ValueError:"Input cannot be empty. Enter a valid number."
>>> check_number_type("Eman")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Detail input assumptions and expand the Raises section to document defensive assertions thoroughly.

Traceback (most recent call last):
...
ValueError:"Please enter a valid number"




"""
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("Please 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:
return "The number is odd"
58 changes: 58 additions & 0 deletions solutions/tests/test_check_number_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""
Unit tests for the check_number_type function.

Test categories:
- Standard cases: typical lists with different lengths
- Edge cases: empty lists, single element
- Defensive tests: wrong input types, assertions


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_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")

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()
Loading