來定義一個計算 x 的N次方的函數:若N<0,則跳出循環,返回s=1的值,是不是需要增加個報錯啥的。
def power(x, n):
? ?s = 1
? ?while n > 0:
? ? ? ?n = n - 1
? ? ? ?s = s * x
? ?return s
print power(5,-1)
def power(x, n):
? ?s = 1
? ?while n > 0:
? ? ? ?n = n - 1
? ? ? ?s = s * x
? ?return s
print power(5,-1)
2017-03-29
舉報
2017-08-08
有一個判斷whlie n >0
2017-03-29
次方數小于零為什么要報錯,3^-2也存在啊
2017-03-29
不需要