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

Add additional doctests, fix grammatical errors for maths/perfect_number.py #12477

Merged
merged 4 commits into from
Dec 28, 2024
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
19 changes: 14 additions & 5 deletions maths/perfect_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,27 @@ def perfect(number: int) -> bool:
False
>>> perfect(-1)
False
>>> perfect(33550336) # Large perfect number
True
>>> perfect(33550337) # Just above a large perfect number
False
>>> perfect(1) # Edge case: 1 is not a perfect number
False
>>> perfect("123") # String representation of a number
Traceback (most recent call last):
...
ValueError: number must be an integer
>>> perfect(12.34)
Traceback (most recent call last):
...
ValueError: number must an integer
ValueError: number must be an integer
>>> perfect("Hello")
Traceback (most recent call last):
...
ValueError: number must an integer
ValueError: number must be an integer
"""
if not isinstance(number, int):
raise ValueError("number must an integer")
raise ValueError("number must be an integer")
if number <= 0:
return False
return sum(i for i in range(1, number // 2 + 1) if number % i == 0) == number
Expand All @@ -70,8 +80,7 @@ def perfect(number: int) -> bool:
try:
number = int(input("Enter a positive integer: ").strip())
except ValueError:
msg = "number must an integer"
print(msg)
msg = "number must be an integer"
raise ValueError(msg)

print(f"{number} is {'' if perfect(number) else 'not '}a Perfect Number.")
Loading