Am trying to write one factorial program in python, in which if I am giving input values as starting value of 1 and count 5 , it should give 1,2,6,24,120 .. but am facing logic error while executing could some help me where am doing the mistake
def getting_input():
while True:
try:
x=int(input("Enter the value:"))
return x
except Exception as e:
print(f"error :{e}")
continue
class factorial:
def __init__(self):
self.x = getting_input()
self.y = getting_input()
def fact(self):
for i in range(self.x,self.y):
c=list()
self.result=1
while i==0 or i==1:
return 1
else:
return i * fact(i-1)
self.result=fact(self)
c.append(self.result)
d={'factorial': c}
print(d)
obj=factorial()
obj.fact()
Output:
Enter the value:1
Enter the value:5
Traceback (most recent call last):
File "C:/Users/Desktop/TOM/f.py", line 27, in <module>
obj.fact()
File "C:/Users/Desktop/TOM/f.py", line 21, in fact
return i * fact(i-1)
NameError: name 'fact' is not defined
Attachments area
factmethod inside the class you may doself.fact()