-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improving the program, making it more modular, real world, pragmatic,…
… renamed it to snake_case, etc...
- Loading branch information
1 parent
05ac4be
commit 02af73d
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import sys | ||
|
||
def get_integer_input(prompt, attempts): | ||
for i in range(attempts, 0, -1): | ||
try: | ||
n = int(input(prompt)) | ||
return n | ||
except ValueError: | ||
print("Enter an integer only") | ||
print(f"{i-1} {'chance' if i-1 == 1 else 'chances'} left") | ||
return None | ||
|
||
def sum_of_digits(n): | ||
total = 0 | ||
while n > 0: | ||
total += n % 10 | ||
n //= 10 | ||
return total | ||
|
||
chances = 3 | ||
number = get_integer_input("Enter a number: ", chances) | ||
|
||
if number is None: | ||
print("You've used all your chances.") | ||
sys.exit() | ||
|
||
result = sum_of_digits(number) | ||
print(f"The sum of the digits of {number} is: {result}") |