python缩进区别分析

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBオリジナル
2016-06-06 11:29:111201ブラウズ

仔细观察下面两个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编写代码时,不能随意的缩进

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。