Discuss / Python / 乘方

乘方

Topic source
def power(x, n):
    s = 1
    while n > 0:
        n = n - 1
        s = s * x
    return s

?? 虽然理解也明白 但是

def power(x, n):
    s = x ** n
    return s

不香么?

茂浚

#2 Created at ... [Delete] [Delete and Lock User]

卧槽?! 原来如此,唉,我这个脑子啊……

彭伊利

#3 Created at ... [Delete] [Delete and Lock User]

我也是!写了x ** n后一看,怎么大家都是用while循环。。。

3838战小柏

#4 Created at ... [Delete] [Delete and Lock User]

def product(x , y = 1, c = 1, d = 1):

    return x * y * c * d

看到评论 我笑了 有那么复杂吗 我这几句就解决了

哎呀博雅

#5 Created at ... [Delete] [Delete and Lock User]

3838战小柏

#4 Created at 2020/2/24 15:39

def product(x , y = 1, c = 1, d = 1):

    return x * y * c * d

看到评论 我笑了 有那么复杂吗 我这几句就解决了

你这是个啥? 算幂的??? 还是算乘法口诀的?

作者的意思是让大家理解这个函数的运作原理,前面的课程中也没有讲到幂的用法,但是讲到了whil的用法,所以代入感会强点。如果这时候讲到这,在讲幂,估计好多小白要疯掉了,什么*xx 什么**xx  为啥还有个x = x**n…… 这些**确实不是骂人的吼~~~~

你品,你细品~~


  • 1

Reply