首页 > 其他 > 详细

while-else

时间:2017-10-20 18:12:42      阅读:235      评论:0      收藏:0      [点我收藏+]
1 #!/usr/bin/env python
  2 #encoding: utf-8
  3 #求给定范围内每个值得最大公约数
  4 #分析:1不是最小公约数,最小公约数>=2,那么最大公约数<=x/2
  5 #定义医德函数,将方法封装
  6 def showMaxFactor(num):
  7     count = num / 2
  8     while count > 1:
  9         if num % count == 0:
 10             print largest factor of %d is %d  % (num,count)
 11             break            #找到公约数后停止循环。
 12         count -= 1
 13     else:
 14         print num, is prime  #没有执行break,才会打印else下面语句
 15 for eachNum in range(10,21):
 16     showMaxFactor(eachNum)

执行结果:

[root@7 script]# python 1.py
largest factor of 10 is 5 
11 is prime
largest factor of 12 is 6 
13 is prime
largest factor of 14 is 7 
largest factor of 15 is 5 
largest factor of 16 is 8 
17 is prime
largest factor of 18 is 9 
19 is prime
largest factor of 20 is 10 

 

while-else

原文:http://www.cnblogs.com/yangmingxianshen/p/7698046.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!