为什么我的代码不返回任何内容
问题描述:
目前对编程和尝试学习 Python 还很陌生.我有这个代码,但我不明白为什么我没有得到返回值:(
fairly new to programming and trying to learn Python at the moment. I have this code and I don't understand why I don't get a return value :(
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()
答
您必须明确使用 return
关键字.可能是你目前有 print c
的地方.
You have to explicitly use the return
keyword. Probably where you currently have print c
.
f
需要在while循环后返回ba
.
f
needs to return ba
after the while loop.