仔細觀察下面兩個python程序,代碼一模一樣,但是運行的結果卻不同,就是因為最后一行return縮進的不同
代碼如下:
def powersum(power, *args):
'''Return the sum of each argument raised to specified power.'''
total = 0
for i in args:
total += pow(i, power)
return total
運行時輸入powersum(2,3,4)輸出25(3的平方加上4的平方)
代碼如下:
def powersum(power, *args):
'''Return the sum of each argument raised to specified power.'''
total = 0
for i in args:
total += pow(i, power)
return total
運行時輸入powersum(2,3,4)輸出9(3的平方)
由此可見,對于python編寫代碼時,不能隨意的縮進
新聞熱點
疑難解答