``##当基本案例在递归终止时执行时,代码需要返回接收者的值
import math
days = int(input())
def adv(days,recipient,likes,depth):
depth+=1
print(" "*depth + "adv({},{})at day {} and likes are {}".format(days-4,recipient,days,likes))
days -= 1
if days == 0:
#print(recipient)
print(likes)
#print(" "*depth + "adv({},{})at day {} and likes are {}".format(days-4,recipient,days,likes))
,在这里它返回接收者`
返回收件人
其他:
点赞=点赞+ math.floor(接收者/ 2)
adv(天数,数学。底数(收件人/ 2)* 3,喜欢的深度)
#该变量a应该接收到接收者的值,但
a = adv(天+1,5,0,1)
打印(a)
答案 0 :(得分:0)
这是因为它打算用作递归函数,但是当您从内部调用它时,您不会返回该调用。相反,当从内部调用时,最里面的返回值将被丢弃,并且None将被传递并由原始调用返回。您可以这样修复它:
import math
days = int(input())
def adv(days,recipient,likes,depth):
depth+=1
print(" "*depth + "adv({},{})at day {} and likes are {}".format(days-4,recipient,days,likes))
days -= 1
if days == 0:
return recipient
else:
likes =likes+ math.floor(recipient/2)
return adv(days,math.floor(recipient/2)*3,likes,depth) #<------
a= adv(days+1,5,0,1)
print(a)
输出:
5
adv(2,5)at day 6 and likes are 0
adv(1,6)at day 5 and likes are 2
adv(0,9)at day 4 and likes are 5
adv(-1,12)at day 3 and likes are 9
adv(-2,18)at day 2 and likes are 15
adv(-3,27)at day 1 and likes are 24
27