need help with python ( no loops used please!) Write a recursive function named
ID: 3757349 • Letter: N
Question
need help with python ( no loops used please!)
Write a recursive function named sum_digits that takes a given a non-negative integer N and returns the sum of its digits. Hint: Mod (%) by 10 gives you the rightmost digit (126 % 10 is 6), while doing integer division by 10 removes the rightmost digit (126/10 is 12). This function has to be recursive; you are not allowed to use loops to solve this problem! This function takes in one integer and returns one intege. For example: Test Result print (234, ": ", sum_digits (234)) 234: 9Explanation / Answer
Ans) def sum_digits(n): if n == 0: return 0 else: return sum_digits(n//10) + (n%10)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.