# File: sumdigits.py # compute and print the sum of the digits of a number # # Matt Bishop, MHI 289I, Fall 2019 # # # sum the digits in n # def sumdigit(n): # base case: single digit, return its value if n < 10: return n # recursive case: several digits, grab the right one and # add it to the sum of the rest return (n % 10) + sumdigit(n // 10) # # the main routine # # read in the number try: n = int(input("A non-negative, preferably multidigit, integer please: ")) except EOFError: pass except: print("I said an integer!") else: # check for error if n < 0: print("I said non-negative!") else: # print the result! print("The sum of the digits in", n, "is", sumdigit(n))