Skip to content

Commit fdb263e

Browse files
authored
Update sum_of_digits_of_a_number.py
Allow decimals
1 parent 85cc5bc commit fdb263e

File tree

1 file changed

+21
-28
lines changed

1 file changed

+21
-28
lines changed

sum_of_digits_of_a_number.py

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
A simple program to calculate the sum of digits of a user-input integer.
2+
A simple program to calculate the sum of digits of a user-input number (integer or decimal).
33
44
Features:
55
- Input validation with limited attempts.
@@ -11,73 +11,66 @@
1111
6
1212
>>> sum_of_digits(0)
1313
0
14-
>>> sum_of_digits(999)
14+
>>> sum_of_digits(999.45)
1515
27
16-
>>> sum_of_digits(-123)
17-
6
16+
>>> sum_of_digits(-123.56)
17+
17
1818
"""
1919

2020
import sys
2121

2222

23-
def get_integer_input(prompt: str, attempts: int) -> int | None:
23+
def get_number_input(prompt: str, attempts: int) -> float | None:
2424
"""
25-
Prompt the user for an integer input, retrying up to a given number of attempts.
25+
Prompt the user for a number (int or float) input, retrying up to a given number of attempts.
2626
2727
Args:
2828
prompt: The message shown to the user.
2929
attempts: Maximum number of input attempts.
3030
3131
Returns:
32-
The integer entered by the user, or None if all attempts fail.
33-
34-
Example:
35-
User input: "12" -> returns 12
32+
The number entered by the user, or None if all attempts fail.
3633
"""
3734
for i in range(attempts, 0, -1):
3835
try:
39-
# Attempt to parse user input as integer
40-
n = int(input(prompt))
36+
n = float(input(prompt))
4137
return n
4238
except ValueError:
43-
# Invalid input: notify and decrement chances
44-
print("Enter an integer only")
39+
print("Enter a valid number only")
4540
print(f"{i - 1} {'chance' if i - 1 == 1 else 'chances'} left")
4641
return None
4742

4843

49-
def sum_of_digits(n: int) -> int:
44+
def sum_of_digits(n: float) -> int:
5045
"""
51-
Compute the sum of the digits of an integer.
46+
Compute the sum of digits of a number, ignoring signs and decimal points.
5247
5348
Args:
54-
n: Non-negative integer.
55-
If the integer is negative, it is converted to positive before computing the sum.
49+
n: Number (int or float)
5650
5751
Returns:
5852
Sum of digits of the number.
5953
6054
Examples:
6155
>>> sum_of_digits(123)
6256
6
63-
>>> sum_of_digits(405)
64-
9
65-
>>> sum_of_digits(-789)
66-
24
57+
>>> sum_of_digits(405.2)
58+
11
59+
>>> sum_of_digits(-789.56)
60+
35
6761
"""
68-
n = abs(n) # FIX: handle negative numbers
62+
n_str = str(abs(n)) # convert to string, remove negative sign
6963
total = 0
70-
while n > 0:
71-
# Add last digit and remove it from n
72-
total += n % 10
73-
n //= 10
64+
for ch in n_str:
65+
if ch.isdigit():
66+
total += int(ch)
7467
return total
7568

7669

7770
def main() -> None:
7871
"""Main entry point of the program."""
7972
chances = 3
80-
number = get_integer_input("Enter a number: ", chances)
73+
number = get_number_input("Enter a number: ", chances)
8174

8275
if number is None:
8376
print("You've used all your chances.")

0 commit comments

Comments
 (0)