Python Forum
Python beginner - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python beginner (/thread-11119.html)

Pages: 1 2


Python beginner - BigDisAok - Jun-23-2018

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 :)


RE: Python beginner - Nwb - Jun-23-2018

months*(borrowed * (interest/12 +1)-repayment))?


RE: Python beginner - BigDisAok - Jun-23-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.


RE: Python beginner - gontajones - Jun-23-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)



RE: Python beginner - BigDisAok - Jun-23-2018

repayment is subtracted after interest has been added.


RE: Python beginner - gontajones - Jun-23-2018

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.


RE: Python beginner - Grok_It - Jun-24-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.


RE: Python beginner - gontajones - Jun-24-2018

Quote:Why on earth would you eliminate a range count in favor of a while loop?
Just to be clearer.


RE: Python beginner - Grok_It - Jun-24-2018

(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.


RE: Python beginner - gontajones - Jun-24-2018

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.