[Tutor] puzzled again by decimal module

Dick Moores rdm at rcblue.com
Fri Aug 18 23:23:50 CEST 2006

As an exercise that I thought would help me understand the decimal 
module, I've been trying write a script (precisionFactorial.py) that 
uses a modified fact(n) to compute precise factorials using the 
decimal module. I''m getting nowhere fast, and don't understand why. 
Here's what I have so far:

def fact(n):
     compute n!
     product = 1
     while n > 1:
       product *= n
       n -= 1
     return product

# precisionFactorial.py

import decimal

def d(x):
     return decimal.Decimal(str(x))

def fact(n):
     product = 1
     while d(n) > 1:
         product *= n
         d(n) -= 1
     return product

n = 100
decimal.getcontext().prec = 200
print product

This gets me

"Syntax error
Theres an error in your program.
*** can't assign to function call
(precisionFactorial.py, line 11)"

line 11 is "d(n) -= 1".

Advice, please.

Dick Moores
rdm at rcblue.com

Win XP, Python 2.4

More information about the Tutor mailing list