Python Forum

Full Version: Python beginner
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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 :)
months*(borrowed * (interest/12 +1)-repayment))?
(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.
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)
repayment is subtracted after interest has been added.
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.
(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.
Quote:Why on earth would you eliminate a range count in favor of a while loop?
Just to be clearer.
(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.
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.
Pages: 1 2