Skip to content

Commit

Permalink
Improving the program, making it more modular, real world, pragmatic,…
Browse files Browse the repository at this point in the history
… renamed it to snake_case, etc...
  • Loading branch information
NitkarshChourasia committed Aug 17, 2023
1 parent 05ac4be commit 02af73d
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions sum_of_digits_of_a_number.py
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}")

0 comments on commit 02af73d

Please sign in to comment.