forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sum of digits of a number.py
33 lines (26 loc) · 1.46 KB
/
Sum of digits of a number.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Python code to calculate the sum of digits of a number, by taking number input from user.
import sys
def get_integer():
for i in range(3,0,-1): # executes the loop 3 times. Giving 3 chances to the user.
num = input("enter a number:")
if num.isnumeric(): # checks if entered input is an integer string or not.
num = int(num) # converting integer string to integer. And returns it to where function is called.
return num
else:
print("enter integer only")
print(f'{i-1} chances are left' if (i-1)>1 else f'{i-1} chance is left') # prints if user entered wrong input and chances left.
continue
def addition(num):
Sum=0
if type(num) is type(None): # Checks if number type is none or not. If type is none program exits.
print("Try again!")
sys.exit()
while num > 0: # Addition- adding the digits in the number.
digit = int(num % 10)
Sum += digit
num /= 10
return Sum # Returns sum to where the function is called.
if __name__ == '__main__': # this is used to overcome the problems while importing this file.
number = get_integer()
Sum = addition(number)
print(f'Sum of digits of {number} is {Sum}') # Prints the sum