Repository: TheAlgorithms/Python

Issue:
Improve our test coverage
### Feature description

Many of our existing algorithm files have little to no unit testing. This is problematic because this can easily let bugs slip through. We want some assurance that the code we currently have is correct and functional. We welcome all contributors to open PRs to help us add tests to our codebase.

### How to find low-coverage files

Go to the Actions tab in this repository and find the most recent **build** workflow run. Open the logs under "Run Tests" and scroll down until you find the section on code coverage:
```
---------- coverage: platform linux, python 3.12.0-final-0 -----------
Name                                                                          Stmts   Miss  Cover   Missing
-----------------------------------------------------------------------------------------------------------
quantum/q_fourier_transform.py                                                   30     30     0%   14-93
scripts/validate_solutions.py                                                    54     54     0%   2-94
strings/min_cost_string_conversion.py                                            78     75     4%   20-57, 61-75, 79-129
...
```
The "Cover" column tells you what percentage of the lines in that file are covered by tests. We want to increase this percentage for existing files. Find a file with low coverage percentage that you wish to write tests for, add doctests for each function, and open a PR with your changes. You do not need to have a perfect coverage percentage, but all functions should have doctests.

Some files will naturally be hard to write tests for. For example, the file may be poorly written because they lack any functions. Other files might be how-tos, meaning they simply demonstrate how to use an existing library's functions rather than implementing the algorithm themselves. Ignore these kinds of files, as they will need to be rewritten eventually. Furthermore, ignore files in the `web_programming` and `project_euler` directories. Web programming files are inherently hard to test and Project Euler files have their own validation workflow, so don't worry about their test coverage.

_**When you open your PR, put "Contributes to #9943" in the PR description.**_ Do not use the word "fixes", "resolves", or "closes". This issue is an ongoing one, and your PR will not single-handedly resolve this issue.

### How to add doctests

A doctest is a unit test that is contained within the documentation comment (docstring) for a function. Here is an example of what doctests look like within a docstring:
```py
def add(a: int, b: int) -> int:
    """
    Adds two non-negative numbers.
    >>> add(1, 1)
    2
    >>> add(2, 5)
    7
    >>> add(1, 0)
    1
    >>> add(-1, -1)
    Traceback (most recent last):
    ...
    ValueError: Numbers must be non-negative
    """
```
For every function in the file you choose, you should write doctests like the ones shown above in its docstring. If a function doesn't have a docstring, add one. Your doctests should be comprehensive but not excessive: you should write just enough tests to cover all basic cases as well as all edge cases (e.g., negative numbers, empty lists, etc).

Do not simply run a function on some example inputs and put its output as the expected output for a doctest. This assumes that the function is implemented correctly when it might not be. Verify independently that your doctests and their expected outputs are correct. **Your PR will not be merged if it has failing tests.** If you happen to discover a bug while writing doctests, please fix it.

_**Please read our [contributing guidelines](https://github.com/TheAlgorithms/Python/blob/master/CONTRIBUTING.md) before you contribute.**_


Discussion:
I'll work on the palindrome script
Hi. I am new and would like to contribute to this issue
Please check:
* #10135
* #10156 
@tianyizheng02 Can you please Review this two PRs

> [Adding doctests in simpson_rule.py](https://github.com/TheAlgorithms/Python/pull/10269)

> [Adding doctests in longest_sub_array.py](https://github.com/TheAlgorithms/Python/pull/10194)
Can we close this issue because it is already merged.
No, improving test coverage is an _ongoing_ issue. It can't be resolved by a single PR because there are many, many files that need tests. Right now, it definitely hasn't been resolved.
@tianyizheng02 Can you please review this PR 

- #10540 
Can we ask people to use "Ref" instead "Fixes", so that we don't have keep reopening this issue?
How is the percentage determined? The tests I added only bumped the coverage by 1%

Resolving patch:
diff --git a/maths/perfect_number.py b/maths/perfect_number.py
new file mode 100644
--- /dev/null
+++ b/maths/perfect_number.py
@@ -0,0 +1,77 @@
+"""
+== Perfect Number ==
+In number theory, a perfect number is a positive integer that is equal to the sum of
+its positive divisors, excluding the number itself.
+For example: 6 ==> divisors[1, 2, 3, 6]
+    Excluding 6, the sum(divisors) is 1 + 2 + 3 = 6
+    So, 6 is a Perfect Number
+
+Other examples of Perfect Numbers: 28, 486, ...
+
+https://en.wikipedia.org/wiki/Perfect_number
+"""
+
+
+def perfect(number: int) -> bool:
+    """
+    Check if a number is a perfect number.
+
+    A perfect number is a positive integer that is equal to the sum of its proper
+    divisors (excluding itself).
+
+    Args:
+        number: The number to be checked.
+
+    Returns:
+        True if the number is a perfect number otherwise, False.
+    Start from 1 because dividing by 0 will raise ZeroDivisionError.
+    A number at most can be divisible by the half of the number except the number
+    itself. For example, 6 is at most can be divisible by 3 except by 6 itself.
+    Examples:
+    >>> perfect(27)
+    False
+    >>> perfect(28)
+    True
+    >>> perfect(29)
+    False
+    >>> perfect(6)
+    True
+    >>> perfect(12)
+    False
+    >>> perfect(496)
+    True
+    >>> perfect(8128)
+    True
+    >>> perfect(0)
+    False
+    >>> perfect(-1)
+    False
+    >>> perfect(12.34)
+    Traceback (most recent call last):
+      ...
+    ValueError: number must an integer
+    >>> perfect("Hello")
+    Traceback (most recent call last):
+      ...
+    ValueError: number must an integer
+    """
+    if not isinstance(number, int):
+        raise ValueError("number must an integer")
+    if number <= 0:
+        return False
+    return sum(i for i in range(1, number // 2 + 1) if number % i == 0) == number
+
+
+if __name__ == "__main__":
+    from doctest import testmod
+
+    testmod()
+    print("Program to check whether a number is a Perfect number or not...")
+    try:
+        number = int(input("Enter a positive integer: ").strip())
+    except ValueError:
+        msg = "number must an integer"
+        print(msg)
+        raise ValueError(msg)
+
+    print(f"{number} is {'' if perfect(number) else 'not '}a Perfect Number.")
diff --git a/maths/special_numbers/perfect_number.py b/maths/special_numbers/perfect_number.py
--- a/maths/special_numbers/perfect_number.py
+++ b/maths/special_numbers/perfect_number.py
@@ -14,16 +14,37 @@
 
 def perfect(number: int) -> bool:
     """
+    Check if a number is a perfect number.
+
+    A perfect number is a positive integer that is equal to the sum of its proper
+    divisors (excluding itself).
+
+    Args:
+        number: The number to be checked.
+
+    Returns:
+        True if the number is a perfect number, False otherwise.
+
+    Examples:
     >>> perfect(27)
     False
     >>> perfect(28)
     True
     >>> perfect(29)
     False
-
-    Start from 1 because dividing by 0 will raise ZeroDivisionError.
-    A number at most can be divisible by the half of the number except the number
-    itself.  For example, 6 is at most can be divisible by 3 except by 6 itself.
+    >>> perfect(6)
+    True
+    >>> perfect(12)
+    False
+    >>> perfect(496)
+    True
+    >>> perfect(8128)
+    True
+    >>> perfect(0)
+    >>> perfect(-3)
+    >>> perfect(12.34)
+    >>> perfect("day")
+    >>> perfect(["call"])
     """
     return sum(i for i in range(1, number // 2 + 1) if number % i == 0) == number
 


Test patch: