Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Q: Assistance in to Write a python class definition for class ‘Sumi’ with a sing

ID: 3819000 • Letter: Q

Question

Q: Assistance 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. Example:

Given 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

Explanation / Answer

Your example and question mis match so I have done based on example as seems like that would be your understanding here.

# pastebin link for code: https://pastebin.com/gH3bzdTC

class Sumi(object):
def __init__(self, n = 0):
self.num = n

def sum_of_digits(sumi):
sum = 0
n = sumi.num
while(n != 0):
sum += n%10
n = n/10
return sum

n = Sumi(369)
print(sum_of_digits(n))

m = Sumi()
print(sum_of_digits(m))