Question: Q: Help in to Write a Python class definition for class Sumi with a single instance variable self.num of type int and single instance method
Q: Help in to Write a Python class definition for class Sumi with a single instance variable self.num of type int and single instance method called sum_of_digits. The default constructor sets the instance variable equal to zero. The instance method returns the sum of digits of the instance.Please note that I do not know the number of digits in advance. This class has to have a RECURSIVE (to call itself):
Given is the Information to make this Python class:
>>> n = Sumi(369)
>>> sum_of_digits(n)
18 >>> m = Sumi() >>> sum_of_digits(m) 0 Hint: use the binary operators // and %. Each method of a class has to have self as a first parameter, i.e. do this: def recur(self, num). Make sure you use self, when you call the method recursively. Remember: The Three Rules of Recursion Base (termination) condition Decomposition to smaller instance Use solutions to smaller instances to solve the original problem
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
