-3

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
gmuraleekrishna
  • 3,375
  • 1
  • 27
  • 45
Vrisk
  • 221
  • 1
  • 11
  • Please fix the indentation and include the complete exception traceback. Also, you can properly divide if you add `from __future__ import division` at the top of the script. – Aran-Fey Dec 20 '14 at 14:22
  • If `c >= n` then you don't enter the `while` loop. You can do the rest I think. – David Heffernan Dec 20 '14 at 14:24

2 Answers2

1

There are a couple of bugs:

  • q isn't defined anywhere before it's used in e = div(1,fact(m)) + q
  • You don't assign the result of round(a,5) to anything.
  • If the while c < n: loop isn't entered, p won't be defined when p = p*n is executed.
  • Both the fact and the div functions don't return anything. (They implicitly return None.)
  • There's no need to check if m >= 20:.
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
  • Okay , I initialized the loop with p = 1 , set q = 0 .. now what ? Fact(3) does return 6 {I think I'm misunderstanding what return means} – Vrisk Dec 20 '14 at 14:38
  • There's a difference between printing and returning. `print(value)` _displays_ a value to the user. `return value` (inside a function) produces `value` as the result to calling the function. See [the docs](https://docs.python.org/2/tutorial/controlflow.html#defining-functions) for more info about functions. – Aran-Fey Dec 20 '14 at 14:44
  • Thanks for your time , I think I finally got this . – Vrisk Dec 20 '14 at 15:00
  • @AnirudhGanesh If Rawing's answer helped, please mark it as accepted using the tick on the left. This lets others know your question has been answered so they can help someone else instead. Accepting and Upvoting also give reputation which is one way to thank someone for their effort solving your problem. – Basic Dec 20 '14 at 15:12
0

See, first of all there are already some cleaner and descent inbuilt methods in Python for calculating the factorial.

import math 
print math.factorial(3)     #6
print math.factorial(4)     #24

And if you want to get the float value after dividing two integers then, you can simply typecast any one of them to the float, it is not necessary to convert both of them.

float_ans = p/float(q)
#or
float_ans = float(p)/q

Using this information, you can calculate the value of e, by simply:

 #Knowing the fact that e = 1 + 1/1! + 1/2! + 1/3! + 1/4! + 1/5! ....
    import math
    e = 0
    """
    As factorial of 20 is a very large value and you won't require that much of
    precision, I have cut down the no of iterations from 20 to 7 only, it also gives
    you a fair amount of precision, you can always change the range to increase and
    decrease the precision accordingly.
    """
    for num in range(0,7):    
        e+=(float(1)/math.factorial(num))
    print e

2.71805555556

ZdaR
  • 22,343
  • 7
  • 66
  • 87