diff --git a/Sum of digits of a number.py b/Sum of digits of a number.py index ba111336965..ce141531e66 100644 --- a/Sum of digits of a number.py +++ b/Sum of digits of a number.py @@ -24,16 +24,27 @@ def get_integer(): def addition(num): + """ + Returns the sum of the digits of a number. + Negative numbers are handled using the absolute value. + + Examples: + >>> addition(123) + 6 + >>> addition(-784) + 19 + """ Sum = 0 if type(num) is type( None ): # Checks if number type is none or not. If type is none program exits. print("Try again!") sys.exit() + num = abs(num) # Handle negative numbers while num > 0: # Addition- adding the digits in the number. digit = int(num % 10) Sum += digit - num /= 10 + num //= 10 return Sum # Returns sum to where the function is called. @@ -42,4 +53,5 @@ def addition(num): ): # this is used to overcome the problems while importing this file. number = get_integer() Sum = addition(number) - print(f"Sum of digits of {number} is {Sum}") # Prints the sum + abs_display = f" (absolute value: {abs(number)})" if number < 0 else "" + print(f"Sum of digits of {number}{abs_display} is {Sum}") # Prints the sum diff --git a/requirements_with_versions.txt b/requirements_with_versions.txt index 84c4b4e6d42..eddfb5ef644 100644 --- a/requirements_with_versions.txt +++ b/requirements_with_versions.txt @@ -83,7 +83,7 @@ pynput==1.8.1 gTTS==2.5.4 ccxt==4.5.22 fitz==0.0.1.dev2 -fastapi==0.122.0 +fastapi==0.123.4 Django==5.2.7 docx==0.2.4 matplotlib==3.10.7