forked from foxli180/BeginningPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpersonal-factorial-iter.py
More file actions
46 lines (35 loc) · 1.04 KB
/
personal-factorial-iter.py
File metadata and controls
46 lines (35 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class factorial():
def __init__(self,n):
self.a = 1
self.max = n
self.b = self.a
#def __next__(self):
# self.a, self.b = self.a+1, self.a *(self.a+1)
# if self.a >= self.max: raise StopIteration
# return self.b
def __next__(self):
if self.a >= self.max:raise StopIteration
self.a = self.a+1
self.b = self.b*self.a
return self.b
def __iter__(self):
return self
class power():
def __init__(self,x,n):
self.x = x
self.n = n
self.a = 1
self.b = x
def __next__(self):
if self.a >= self.n:raise StopIteration
self.a = self.a +1
self.b = self.b * self.x
return self.b
def __iter__ (self):
return self
def main():
fac = factorial(5)
print (list(fac)[-1])
powers = power(2,2)
print (list(powers))
if __name__ =='__main__':main()