a2421480 发表于 2018-12-8 18:27:37

看代码

使用递归编写一个 power() 函数模拟内建函数 pow(),即 power(x, y) 为计算并返回 x 的 y 次幂的值。
def power(x, y):
    if y:
      return x * power(x, y-1)
    else:
      return 1
   
print(power(2, 3)) 这模板怎么没有pow(),什么叫内建函数?

冬雪雪冬 发表于 2018-12-8 18:37:29

python系统自带的函数,如print,input,pow等都是内置函数(built-in function)

a2421480 发表于 2018-12-8 19:59:53

冬雪雪冬 发表于 2018-12-8 18:37
python系统自带的函数,如print,input,pow等都是内置函数(built-in function)

这个函数是怎么运行变成8的呢

冬雪雪冬 发表于 2018-12-8 20:04:49

power(2, 3)
2 * power(2, 2)
2 * 2 * power(2, 1)
2 * 2 * 2 * power(2, 0)
2 * 2 * 2 * 1
8

a2421480 发表于 2018-12-8 20:05:32

冬雪雪冬 发表于 2018-12-8 20:04
power(2, 3)
2 * power(2, 2)
2 * 2 * power(2, 1)


谢谢老师
页: [1]
查看完整版本: 看代码