嵌入式函数返回None

嵌入式函数返回None

本文介绍了嵌入式函数返回None的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  def parameter_function(principal,annual_interest_rate,duration):
n = float(duration * 12)
如果annual_interest_rate == 0:
r = float(principal / n)
其他:
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))

结果= monthly_payment_function p,r,n)
返回结果


解决方案

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

你还有一个无条件的 return ,在 def monthly_payment_function 之前,这意味着它永远不会被调用说话,它甚至从来没有被定义)。



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

  from __future__ import division#Python 2.x:int / int给出float b 
$ b MONTHS_PER_YEAR = 12

def month_payment(本金,pct_per_year,年):
月=年* MONTHS_PER_YEAR
如果pct_per_year == 0:
返回本金/月
其他:
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_compounde d


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
解决方案

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

这篇关于嵌入式函数返回None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 09:47