python缩进区别分析

yipeiwu_com6年前Python基础

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

相关文章

使用Python进行AES加密和解密的示例代码

高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代...

python 读取视频,处理后,实时计算帧数fps的方法

实时计算每秒的帧数 cap = cv2.VideoCapture("DJI_0008.MOV") #cap = cv2.VideoCapture(0) # Define the...

Python实现重建二叉树的三种方法详解

Python实现重建二叉树的三种方法详解

本文实例讲述了Python实现重建二叉树的三种方法。分享给大家供大家参考,具体如下: 学习算法中,探寻重建二叉树的方法: 用input 前序遍历顺序输入字符重建 前序遍历顺序字...

python实现颜色rgb和hex相互转换的函数

本文实例讲述了python实现颜色rgb和hex相互转换的函数。分享给大家供大家参考。具体分析如下: 下面的python代码提供了两个函数分别用来将rgb表示的颜色转换成hex值,hex...

python多线程方式执行多个bat代码

python多线程方式执行多个bat的代码,感兴趣的朋友可以参考下。 import threading from win32api import * class MyThread...