您的位置:首页 > 其它

tuple demo

2015-07-08 08:20 357 查看
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: