嵌入式函数返回无

时间:2016-02-13 02:37:23

标签: python function

我的函数返回None。我已经检查以确保所有操作都是正确的,并且我对每个函数都有一个return语句。

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

1 个答案:

答案 0 :(得分:2)

monthly_payment_function不会返回任何内容。将monthly_payment=替换为return(即“返回”后跟空格)。

此外,您在return之前有一个无条件def monthly_payment_function,这意味着它永远不会被调用(严格来说,它甚至从未定义过)。

你也是随机混合单位,你的变量名可以使用一些帮助:

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