Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please help with the code
#1
Hi guys, my code should do this:

I have an account of 10000$ and every month I take interest of 0.5% and also I substract 500$ every month
I need for my program to stop when the account reaches 0.
And also all the years until depletion to be listed.

PS: I am a total beginner:)
Please se my code below which is not displaying an output.Please advise.
account = 10000
month = account/0.5-500
year = 12*month

for i in range(account):
    if account == account/0.5-500:
        if account == 0:
            break
        else:
            print(year)
Reply
#2
The interest should be account*0.5
This looks like homework to me
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Hi Wavic thank you for replying.
I am studying for a course and I am trying to resolve all the problems from each chapter from Python for everyone - Cay S. Horstmann and I am stuck with this code.
It is not literally my homework but I think is a problem from the book.
Reply
#4
Your loop is completely wrong. You iterate over account balance which is $10000 so you will do 10000 loops eventually. Instead of years. You check for == to 0 but probability the balans got equal to 0 is minimal. This will fail. Check for <= 0.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
Ok so I have changed my code a bit but is still printing now -139 months which is wrong.


initial_balance = 1000
rate = 10
withdraw = 500
result = 1
target = result
balance = initial_balance
month = 1

while balance>result:
    interest = balance*rate/100
    balance = balance - interest - withdraw
    month = month-interest
    
if balance == month:
    print (month)

print ("no of months is", month)
Reply
#6
You're subtracting the interest. Aren't you supposed to add the interest?

Also, you don't want to subtract the interest from the month, you want to increase the month by one each time through the loop. You can do that with month += 1.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#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
#8
Hm, is that value (0.5) a percentage per month? If it is you should use it in the script but you have to divide it by 100. Now you get $500 per month interests.

account = account + account*(0.5/100) - 500

This add the interest to the balance and subtracts 500.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Forum Jump:

User Panel Messages

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