How to edit this class to obtain n0 in the PPP? I am not obtaining the initial number in the sequence. Thanks
class P():
def __init__(self, n0):
self.n = n0
def __iter__(self):
return self
def __next__(self):
if self.n == 1:
raise StopIteration
self.n = self.n - 1
return self.n
nmax = 10
PP = P(nmax)
PPP = []
for j in PP:
PPP.append(j)
print(PPP)
Current output:
[9, 8, 7, 6, 5, 4, 3, 2, 1]
Desired output:
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
CodePudding user response:
Store the value before decrementing it
def __next__(self):
if self.n == 1:
raise StopIteration
num = self.n
self.n = self.n - 1
return num
#[10, 9, 8, 7, 6, 5, 4, 3, 2]