关闭

tuple DEMO

118人阅读 评论(0) 收藏 举报
分类:
#quote from MIT 'introduction to computation and programming using python, Revised'
def findDivisors(n1, n2):
    """Assumes that n1 and n2 are positive ints
       Returns a tuple containing all common divisors of n1 & n2"""
    divisors = () #the empty tuple
    for i in range(1, min(n1, n2) + 1):
        if n1%i == 0 and n2%i == 0:
            divisors += (i,)
    return divisors
    
divisors = findDivisors(20, 100)
print divisors
total = 0
for d in divisors:
    total += d
print total


%run "C:\Users\Administrator\test.py"
(1, 2, 4, 5, 10, 20)
42

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:84407次
    • 积分:2835
    • 等级:
    • 排名:第12590名
    • 原创:165篇
    • 转载:108篇
    • 译文:30篇
    • 评论:1条
    文章分类
    最新评论