如果单词 return 没有 return 任何东西,为什么要循环?

Why in loop if the word return does not return anything?

def arithmetic(a,b):
    if a == 0 :
        return b
    else:
        arithmetic(a-1,b+a)

print(arithmetic(4,0))

原因是您没有从 else 块中返回值。

def arithmetic(a,b):
   if a == 0 :
      return b
   else:
      return arithmetic(a-1,b+a)

print(arithmetic(4,0))

这是一个巧妙的 one-liner 使用三元 if 语句

def arithmetic(a,b):
    return b if a == 0 else arithmetic(a-1,b+a)