Python

Erik Max Francis max at alcyone.com
Fri Feb 7 04:43:37 EST 2003


Uri wrote:

> Befuddled newbie attempts a program that calculates compound interest,
> using a formula to the effect of:
> 
> FV=P(1+r)^n

This is the hint I go on below.

> Ok, so Python vets I bet are already seeing what my problem is. I ask
> for the numbers with the syntax:     r = input("r?")

It's safer to ask with

	r = float(raw_input("r?"))

> My problem is that r is a percentage (in my program, I tell the user
> to represent 4% as .04).  When I run the program, it says that there's
> no way to deal with a double or something to that effect.

When asking for solutions to technical problems, it's best to be as
specific and technical as possible.  Exactly what code did you run, what
error did you get, and what was the traceback?  Without this
information, our responses can only be guesses.

>From the vague description and your writing of the compound interest
equation, I'm betting that your problem is that you don't realize that
the ^ operator in Python is not exponentiation, it's bitwise xor.  The
operator you want is **.  If you try to use the ^ operator with floats,
you'll get this error, which sounds similar to what you've complained
about:

>>> 1.0 ^ 2.0
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for ^: 'float' and 'float'

Instead, try writing the interest equation in your code as

	FV = P*(1 + r)**n

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ I'll be your strength / I'll be here when you wake up (all right)
\__/ Sweetbox
    7 Sisters Productions / http://www.7sisters.com/
 Web design for the future.




More information about the Python-list mailing list