练习一:定义一个方法打印一个整数的所有因子以及因子个数
def print_factors(num):
factors = []
for i in range(1, num+1):
if num % i == 0:
factors.append(i)
print("该整数的因子为:", factors)
print("共有 %d 个因子" % len(factors))
调用示例:
print_factors(12)
输出结果:
该整数的因子为: [1, 2, 3, 4, 6, 12]
共有 6 个因子
原文地址: https://www.cveoy.top/t/topic/eBw8 著作权归作者所有。请勿转载和采集!