Returns the sum of the digits of a number. Negative numbers are handled using the absolute value. Examples: >>> addition(123) 6 >>> addition(-784) 19
(num)
| 24 | |
| 25 | |
| 26 | def addition(num): |
| 27 | """ |
| 28 | Returns the sum of the digits of a number. |
| 29 | Negative numbers are handled using the absolute value. |
| 30 | |
| 31 | Examples: |
| 32 | >>> addition(123) |
| 33 | 6 |
| 34 | >>> addition(-784) |
| 35 | 19 |
| 36 | """ |
| 37 | Sum = 0 |
| 38 | if type(num) is type( |
| 39 | None |
| 40 | ): # Checks if number type is none or not. If type is none program exits. |
| 41 | print("Try again!") |
| 42 | sys.exit() |
| 43 | num = abs(num) # Handle negative numbers |
| 44 | while num > 0: # Addition- adding the digits in the number. |
| 45 | digit = int(num % 10) |
| 46 | Sum += digit |
| 47 | num //= 10 |
| 48 | return Sum # Returns sum to where the function is called. |
| 49 | |
| 50 | |
| 51 | if ( |
no outgoing calls
no test coverage detected