# File: sumdigits.py # compute and print the sum of the digits of a number # # Matt Bishop, ECS 10, Fall 2012 # # # 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 # def main(): # read in the number try: n = int(input("A non-negative, preferably multidigit, integer please: ")) except: print("I said an integer!") return # check for error if n < 0: print("I said non-negative!") return # print the result! print("The sum of the digits in", n, "is", sumdigit(n)) main()