Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Python Interest Caculator with Withdraw

python interest calculator withdraw lkp

  • Please log in to reply
No replies to this topic

#1 Sundance


    CC Devotee

  • Validating
  • PipPipPipPipPipPip
  • 572 posts
  • Programming Language:C, Java, PHP, Python, JavaScript, Perl, PL/SQL, Transact-SQL, Bash, Others

Posted 11 November 2013 - 05:13 AM

# Simple Interest Calculator
# Only works in Dollars and does not handle Cents
# PY2.7 
# By LKP 
# Spelling mistakes may be rampant.
# Has no error handling but still figured it would be fun to do
# Based off of the mathematics problem from this post
# http://forum.codecall.net/topic/76922-can-somebody-help-me-with-this-question/?p=665676
print "What is your savings amount?" # Queries the amount of savings
savings = raw_input("> ") # sets the savings variable from the users input 
print "What is your yearly interest rate?" # Queries the amount of interest in Percent
interest = raw_input("> ") # Sets the interest variable
print "How much will you withdraw per year?" # queries the amount that will be widthdrawn per year
withdraw = raw_input("> ") # Sets the withdraw variable

trim_s_one = savings.replace("$","") # Takes the dollar symbol off of the input (only if it exists)
trim_s_two = trim_s_one.replace(",","") # Takes the commas off the input
savings_conv = int(trim_s_two) # turns the string into an interger

trim_i_one = interest.replace("%","") # takes off the % sign from the interest variable
interest_conv = int(trim_i_one) # Converts it into an interger too

trim_w_one = withdraw.replace("$","") # Again, takes the dollar sign off of the withdraw variable
trim_w_two = trim_w_one.replace(",","") # Takes the commas off too!
withdraw_conv = int(trim_w_two) # And again converts it into an interger

B = [] # List to append
while savings_conv > withdraw_conv: # while the savings is greater than the widthdraw then loop the code!
	savings_conv = savings_conv - withdraw_conv  # repeatedly subtracts from the savings interger
	interest_generated = savings_conv / 100 * interest_conv # works out how much interest has been accumulated.
	savings_conv = savings_conv + interest_generated # Adds the accumulated interest into the savings
	B.append(str(savings_conv)) # adds each amount of withdrawls to the list
	# This code then counts the amount of variables in the list and that's how many years it will last!
print "You can withdraw $"+withdraw,"for",len(B),"years before your account will be left with $"+B[-1],"left!"

It's not perfect, could have refined the code but I figured it would be a fun thing to do whilst I had a few minutes to spare.

  • 1

Please read the

FaQ & Guidelines

Also tagged with one or more of these keywords: python, interest, calculator, withdraw, lkp

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download