Discuss / Python / 求问关于 函数的参数一节 求X的N 次方的问题,我看不懂那段代码。

求问关于 函数的参数一节 求X的N 次方的问题,我看不懂那段代码。

mythxox

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

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

S 是什么啊。 这段代码 看不懂啊。比如5的 3次方 x等于5 n等于3 第四行 不是N赋值了 2 s =1 然后乘以5 S=5 。。 return s 不是5 吗

mythxox

#2 Created at ... [Delete] [Delete and Lock User]
def power(x, n):
    s = 1
    while n > 0:
        n = n - 1
        s = s * x
    return s

俆木

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

只要n是大于0的,就一直循环运行(n=n-1 s=s*x),当n小于0是才执行return s


  • 1

Reply