为了账号安全,请及时绑定邮箱和手机立即绑定

如何检查每个数字而不是一个值?

如何检查每个数字而不是一个值?

吃鸡游戏 2021-06-28 13:56:42
除数:对于从 2 到 100 的数字,打印一系列行指示哪些数字是其他数字的除数。对于每个,打印出“X 除 Y”,其中 X <= Y,并且 X 和 Y 都在 2 和 100 之间。前几行将是: 2 除以 2 3 除以 3 2 除以 4 4 除以 4 5 除以 5等等。到目前为止我有这个x = 2y = 2while y <= 100:      while y <= 100:            if y % x == 0:                print(x, 'divides', y)                y += 1            elif y % x != 0:                y += 1我不知道如何让它测试 x 和 y 的其他值
查看完整描述

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


查看完整回答
反对 回复 2021-07-06
?
白衣染霜花

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


查看完整回答
反对 回复 2021-07-06
  • 3 回答
  • 0 关注
  • 103 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信