Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

python - Embedded function returns None

My function returns None. I have checked to make sure all the operations are correct, and that I have a return statement for each function.

def parameter_function(principal, annual_interest_rate, duration):
    n = float(duration * 12)
    if annual_interest_rate == 0:
        r = float(principal / n)
    else:
        r = float(annual_interest_rate / 1200)
    p = principal
    return (p, r, n)

    def monthly_payment_function(p, r, n):
        monthly_payment = p * ((r * ((1 + r) ** n)) / (((1 + r) ** n) - 1))

    result = monthly_payment_function(p, r, n)
    return result
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

monthly_payment_function does not return anything. Replace monthly_payment= with return (that's 'return' followed by a space).

Also you have an unconditional return before def monthly_payment_function, meaning it never gets called (strictly speaking, it never even gets defined).

Also you are pretty randomly mixing units, and your variable names could use some help:

from __future__ import division    # Python 2.x: int/int gives float

MONTHS_PER_YEAR = 12

def monthly_payment(principal, pct_per_year, years):
    months = years * MONTHS_PER_YEAR
    if pct_per_year == 0:
        return principal / months
    else:
        rate_per_year   = pct_per_year / 100.
        rate_per_month  = rate_per_year / MONTHS_PER_YEAR
        rate_compounded = (1. + rate_per_month) ** months - 1.
        return principal * rate_per_month * (1. + rate_compounded) / rate_compounded

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...