3 回答
TA贡献1906条经验 获得超10个赞
这应该做一个工作。试一试,如果有什么不清楚的给我喊
x = int(input("Give the range you want to check numbers in: "))
for number in range(1,x):
for value in range(1,number+1):
if number % value == 0:
print(number, " is divided by", value)
输入“10”的输出:
1 is divided by 1
2 is divided by 1
2 is divided by 2
3 is divided by 1
3 is divided by 3
4 is divided by 1
4 is divided by 2
4 is divided by 4
5 is divided by 1
5 is divided by 5
6 is divided by 1
6 is divided by 2
6 is divided by 3
6 is divided by 6
7 is divided by 1
7 is divided by 7
8 is divided by 1
8 is divided by 2
8 is divided by 4
8 is divided by 8
9 is divided by 1
9 is divided by 3
9 is divided by 9
TA贡献1796条经验 获得超10个赞
您可以在此处使用enumerate并在每个项目之前循环遍历索引将产生您想要获得的结果
x = [*range(2, 101)]
for idx, item in enumerate(x):
for i in x[:idx +1]:
if not item % i:
print('{} divides {}'.format(i, item))
2 divides 2
3 divides 3
2 divides 4
4 divides 4
5 divides 5
2 divides 6
3 divides 6
6 divides 6
...
99 divides 99
2 divides 100
4 divides 100
5 divides 100
10 divides 100
20 divides 100
25 divides 100
50 divides 100
100 divides 100
添加回答
举报