-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from 5 commits
72c3e65
70c78fb
ff2bdba
6e253c7
de8e75a
c36b659
148b50e
2570acd
f389067
26f0653
dba5a61
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
""" | ||
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 | ||
Raises: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, |
||
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" | ||
or "Enter a valid number " | ||
Examples : | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
""" | ||
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: | ||
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) |
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 elements | ||
- 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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
explain more for input parameter