Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please help with the code
#7
You should use more descriptive variable names.  Going back to your original parameters, you are going to need five variables:
  1. principle --> how much you have
  2. interest --> the interest rate (really? .5%, nice bank :) )
  3. withdraw --> amount you take out each month
  4. month --> how many months transactions take place
  5. years --> how many years
With that in mind, you start with:

principle = 10000
interest = .5 / 100
withdraw = 500
month = 0.0
years = 0.0
In your conditional loop, you want to repeat a process until the condition is no longer True, in this case, keep repeating until there is not enough money left to cover the withdraw and interest.  Here we add a sixth variable we''l call 'new_interest'.  There are two ways to calculate the principle, subtract the interest then make the withdraw (bank makes money) or make the withdraw then subtract the interest (bank makes less money).  Your conditional should look something like this now:

while principle >= withdraw:
    new_interest = interest * principle
    principle = principle - new_interest - withdraw
    month += 1
and finally:
print("no of years is", round(month / 12, 3))
You could add two additional variables: total_interest and total_withdrawal and test your results:

print('Test = ', round(principle + total_interest + total_withdrawal, 2))
You should come very close, if not spot on to your original principle (may be slightly off due to rounding)
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Messages In This Thread
Please help with the code - by zarnav - Feb-12-2017, 02:15 PM
RE: Please help with the code - by wavic - Feb-12-2017, 02:17 PM
RE: Please help with the code - by zarnav - Feb-12-2017, 02:20 PM
RE: Please help with the code - by wavic - Feb-12-2017, 02:37 PM
RE: Please help with the code - by zarnav - Feb-12-2017, 04:32 PM
RE: Please help with the code - by ichabod801 - Feb-12-2017, 07:11 PM
RE: Please help with the code - by sparkz_alot - Feb-13-2017, 03:09 PM
RE: Please help with the code - by wavic - Feb-13-2017, 03:45 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020