|
1 | 1 | """ |
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). |
3 | 3 |
|
4 | 4 | Features: |
5 | 5 | - Input validation with limited attempts. |
|
11 | 11 | 6 |
12 | 12 | >>> sum_of_digits(0) |
13 | 13 | 0 |
14 | | - >>> sum_of_digits(999) |
| 14 | + >>> sum_of_digits(999.45) |
15 | 15 | 27 |
16 | | - >>> sum_of_digits(-123) |
17 | | - 6 |
| 16 | + >>> sum_of_digits(-123.56) |
| 17 | + 17 |
18 | 18 | """ |
19 | 19 |
|
20 | 20 | import sys |
21 | 21 |
|
22 | 22 |
|
23 | | -def get_integer_input(prompt: str, attempts: int) -> int | None: |
| 23 | +def get_number_input(prompt: str, attempts: int) -> float | None: |
24 | 24 | """ |
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. |
26 | 26 |
|
27 | 27 | Args: |
28 | 28 | prompt: The message shown to the user. |
29 | 29 | attempts: Maximum number of input attempts. |
30 | 30 |
|
31 | 31 | 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. |
36 | 33 | """ |
37 | 34 | for i in range(attempts, 0, -1): |
38 | 35 | try: |
39 | | - # Attempt to parse user input as integer |
40 | | - n = int(input(prompt)) |
| 36 | + n = float(input(prompt)) |
41 | 37 | return n |
42 | 38 | except ValueError: |
43 | | - # Invalid input: notify and decrement chances |
44 | | - print("Enter an integer only") |
| 39 | + print("Enter a valid number only") |
45 | 40 | print(f"{i - 1} {'chance' if i - 1 == 1 else 'chances'} left") |
46 | 41 | return None |
47 | 42 |
|
48 | 43 |
|
49 | | -def sum_of_digits(n: int) -> int: |
| 44 | +def sum_of_digits(n: float) -> int: |
50 | 45 | """ |
51 | | - Compute the sum of the digits of an integer. |
| 46 | + Compute the sum of digits of a number, ignoring signs and decimal points. |
52 | 47 |
|
53 | 48 | 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) |
56 | 50 |
|
57 | 51 | Returns: |
58 | 52 | Sum of digits of the number. |
59 | 53 |
|
60 | 54 | Examples: |
61 | 55 | >>> sum_of_digits(123) |
62 | 56 | 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 |
67 | 61 | """ |
68 | | - n = abs(n) # FIX: handle negative numbers |
| 62 | + n_str = str(abs(n)) # convert to string, remove negative sign |
69 | 63 | 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) |
74 | 67 | return total |
75 | 68 |
|
76 | 69 |
|
77 | 70 | def main() -> None: |
78 | 71 | """Main entry point of the program.""" |
79 | 72 | chances = 3 |
80 | | - number = get_integer_input("Enter a number: ", chances) |
| 73 | + number = get_number_input("Enter a number: ", chances) |
81 | 74 |
|
82 | 75 | if number is None: |
83 | 76 | print("You've used all your chances.") |
|
0 commit comments