Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python beginner
#1
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 :)
Reply
#2
months*(borrowed * (interest/12 +1)-repayment))?
Reply
#3
(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.
Reply
#4
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)
Reply
#5
repayment is subtracted after interest has been added.
Reply
#6
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.
Reply
#7
(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.
Reply
#8
Quote:Why on earth would you eliminate a range count in favor of a while loop?
Just to be clearer.
Reply
#9
(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.
Reply
#10
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.
Reply


Forum Jump:

User Panel Messages

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