Posts: 9
Threads: 3
Joined: Jun 2018
Jun-23-2018, 11:18 AM
(This post was last modified: Jun-23-2018, 11:24 AM by BigDisAok.)
Hi all,
I am new to Python (really enjoying it so far) and am working my way through Learn Python the hard way. I have just learnt about functions and decided to go off piste and make myself a question to solve. I can't find the answer online, so was wondering if you good people can help me.
Problem PART A : I am trying to work out how much of my mortgage is left after X months of repayment. In this case, amount borrowed - 200000, Interest rate 0.03 per anum, time: 72 months, paying back 750 a month:
def mortgage(borrowed, repayment, interest, time):
print(f"total left after {time} months \n")
print(borrowed * (interest/12 +1)-repayment) #this calculates mortgage left after 1 month. How do I repeat this calculation on the answer? 71 times
mortgage(200000,750,.03,72) Many Thanks,
BigD :)
Posts: 75
Threads: 20
Joined: Jun 2018
Jun-23-2018, 11:57 AM
(This post was last modified: Jun-23-2018, 11:58 AM by Nwb.)
months*(borrowed * (interest/12 +1)-repayment))?
Posts: 9
Threads: 3
Joined: Jun 2018
(Jun-23-2018, 11:57 AM)Nwb Wrote: months*(borrowed * (interest/12 +1)-repayment))?
that does not work, because after 1 month the amount owed has reduced and thus the interest owed is less.
Posts: 333
Threads: 4
Joined: Jun 2018
Just check if the repayment has to be after or before the interest calculation.
def mortgage(borrowed, repayment, interest, time):
print(f"total left after {time} months \n")
for m in range(time):
borrowed = borrowed - repayment
borrowed = borrowed * (1 + interest / 12)
print(borrowed)
mortgage(200000, 750, .03, 72)
Posts: 9
Threads: 3
Joined: Jun 2018
repayment is subtracted after interest has been added.
Posts: 333
Threads: 4
Joined: Jun 2018
Jun-23-2018, 01:21 PM
(This post was last modified: Jun-23-2018, 01:21 PM by gontajones.)
So...
def mortgage(borrowed, repayment, interest, time):
print(f"total left after {time} months \n")
while (time > 0):
borrowed = borrowed * (1 + interest / 12)
borrowed = borrowed - repayment
time -= 1
print(borrowed)
mortgage(200000, 750, .03, 72) It's not necessary to use a for loop.
Posts: 16
Threads: 1
Joined: Jun 2018
(Jun-23-2018, 01:21 PM)gontajones Wrote: So...
def mortgage(borrowed, repayment, interest, time):
print(f"total left after {time} months \n")
while (time > 0):
borrowed = borrowed * (1 + interest / 12)
borrowed = borrowed - repayment
time -= 1
print(borrowed)
mortgage(200000, 750, .03, 72) It's not necessary to use a for loop.
Why on earth would you eliminate a range count in favor of a while loop?
Each iteration of a range count takes 3 operations, the equivalent in a while loop takes 10 operations, assuming all internal operations are amalgamated as one.
range is implemented completely in c, while and "time -= 1" are interpreted commands and are very slow comparatively.
Posts: 333
Threads: 4
Joined: Jun 2018
Quote:Why on earth would you eliminate a range count in favor of a while loop?
Just to be clearer.
Posts: 16
Threads: 1
Joined: Jun 2018
Jun-24-2018, 05:27 PM
(This post was last modified: Jun-24-2018, 05:27 PM by Grok_It.)
(Jun-24-2018, 05:08 PM)gontajones Wrote: Quote:Why on earth would you eliminate a range count in favor of a while loop?
Just to be clearer.
for month in range(months):
borrowed *= 1 + interest / 12
borrowed -= repayment
print(borrowed) With a little cleaning and some proper variable names the for loop is perfectly clear, but I do respect wanting to clean up code. I like my code neat too, but never at the cost of speed. Try it for yourself. Run both for 100K months.
Posts: 333
Threads: 4
Joined: Jun 2018
Jun-24-2018, 05:30 PM
(This post was last modified: Jun-24-2018, 05:30 PM by gontajones.)
Quote: for month in range(months):
borrowed -= repayment
borrowed *= (1 + interest / 12)
print(borrowed) With a little cleaning and some proper variable names the for loop is perfectly clear, but I do respect wanting to clean up code. I like my code neat too, but never at the cost of speed. Try it for yourself. Run both for 100K months.
I completely agree with you. But maybe a beginner would read this and ask why the 'month' ('m' in my case) variable exists.
|