【发布时间】:2016-11-26 00:23:04
【问题描述】:
目前对编程和尝试学习 Python 还很陌生。我有这段代码,但我不明白为什么我没有得到返回值:(
balance = 3200
annualInterestRate = 0.2
monthlyInterestRate = (annualInterestRate/12 + 1)
def f(x):
m = 0
ba = balance
while m < 12:
ba = (ba - x)*monthlyInterestRate
m += 1
return ba
def bisection():
a = 0
b = balance
c = (a+b)/2
while a != b:
if f(c) == 0:
return c
elif f(c) < 0:
a = c
else:
b = c
c = (a+b)/2
return c
bisection()
【问题讨论】:
-
你已经定义了一些函数,但从未调用过它们中的任何一个。
-
您要返回什么?我没有调用任何函数。
print c? -
在二等分函数中,我调用了 f(x).. 我真的不知道什么时候应该调用二等分函数。对,就是这样。我需要一个值,以便 12 次循环后 ba 为 0。
-
你既不会从函数中返回东西,也不会调用它们。你为什么期望返回值?