Python

Mike C. Fletcher mcfletch at rogers.com
Fri Feb 7 05:03:59 CET 2003


Use raw_input, and then process the resulting string explicitly. BTW: 
You want the user to input a percentage, so why not let them enter the 
actual percentage?

Anyway, here's a bit of code typed into the interpreter to give you an 
idea where you could go.  By the way, your P(1+r)^n says: "call P with 
argument 1+r, then raise the result to the nth power", you probably want 
an * in there somewhere ;) .

 >>> rawString = raw_input( """Your interest rate (percent)> """ )
 >>> rawString
'  23 %  '
 >>> rawString = rawString.replace( "%", "" )
 >>> rawString = rawString.strip()
 >>> rawString
'23'
 >>> percentage = float( rawString ) # interprets string as a float
 >>> percentage
23.0
 >>> fraction = percentage / 100.0
 >>> fraction
0.23000000000000001
 >>>

BTW, when asking for help on a problem, it's generally a good idea to 
say _exactly_ what error is being reported, not just summarise with 
"something to that effect" ;) .  Including the code that's failing (if 
it's fairly small) is also considered useful, as it means the helper 
doesn't have to reconstruct your application (and your error) just to 
show how to accomplish the task :) .

Good luck, and have fun :) ,
Mike

Uri wrote:

>Befuddled newbie attempts a program that calculates compound interest,
>using a formula to the effect of:
>
>FV=P(1+r)^n
>
>(FV=Final Value, P = principal, r = interest rate, n = number of
>dividends)
>
>Ok, so Python vets I bet are already seeing what my problem is. I ask
>for the numbers with the syntax:     r = 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. But I also
>read that there's no way to type a variable, that they are
>"dynamically typed". Well, that doesn't work here.
>
>So, what do I do?
>
>Thanx in advance.
>-Uri
>(phrogeeb at hotmail.com)
>  
>
_______________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://members.rogers.com/mcfletch/








More information about the Python-list mailing list