为什么会返回“你还欠:没有”

2024-10-02 16:26:35 发布

您现在位置:Python中文网/ 问答频道 /正文

def hotel_cost(nights):
    return nights * 140

bill = hotel_cost(5)

def add_monthly_interest(balance):
    balance * (1 + (0.15 / 12))

def make_payment(payment, balance): 
    new_balance = add_monthly_interest(balance)
    print "You still owe: " + str(new_balance)

make_payment(100,hotel_cost(5))

这是印刷“你还欠:没有”,我觉得我只是错过了一些非常基本的东西。我已经是最新的了。Python是我的第一语言,除了像我们这一代人一样精通技术之外,没有其他真正的计算机知识。你知道吗


Tags: youaddnewmakereturndefpaymenthotel
3条回答

add_monthly_interest需要一个return语句。你知道吗

没有return语句的函数(或者实际上,在执行结束时)返回None。这是发生在:

new_balance = add_monthly_interest(balance)

所以,你得到None,然后打印出来。您希望在该函数中使用return,与其他一些语言不同,python不返回最后一个表达式的值。你知道吗

add_monthly_interest不返回任何内容,因此Python让它自动返回None。必须返回表达式的结果:

def add_monthly_interest(balance):
    return balance * (1 + (0.15 / 12))

相关问题 更多 >