I was trying to compute e (2.718283) in python and I realized you couldn't simply divide so I defined a function to divide and round to five digits and I also defined a function to find factorials , here it is - .
def div(x,y):
t = x + 0.00
p = y + 0.00
a = t / p
round(a,5)
print a
def fact(n):
c = 1
d = 1
while c < n:
p = c * d
d = c*d
c = c + 1
if c >= n:
break
p = p*n
print p
m = 0
while m < 20:
e = div(1,fact(m)) + q
q = div(1,fact(m + 1)) + q
if m >= 20:
break
print e `
I execute it and I get this UnboundLocalError: local variable 'p' referenced before assignment . But fact(3) seems to be working perfectly .. What is going on ?
PS : i'm not yet used to formatting here but I have indented properly in the actual code
EDIT : as requested
line 20, in <module>
e = div(1,fact(m)) + q
File "/home/anirudh/Desktop/TEST PY/Hark-1.py", line 16, in fact
p = p*n
UnboundLocalError: local variable 'p' referenced before assignment