标签:cti his else var split show pen view nes
在python中你可能时不时不碰到else语句用在while和for循环中,请不要吃惊,先看看它的作用吧!
实际上在循环语句中,else子句仅仅会在循环完毕后运行。即跳出循环的操作。如break,同一时候也会跳过
else块。
以下是一个来自python核心编程的样例
def showMaxFactor(num):
count = num/2
while count > 1:
if num%count == 0:
print ‘largest factor of %d is %d‘ % (num,count)
break
count -= 1
else:
print num, "is prime"
for eachNum in range(10,21):
showMaxFactor(eachNum)
标签:cti his else var split show pen view nes
原文地址:http://www.cnblogs.com/gavanwanggw/p/7019356.html