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

除数:对于从 2 到 100 的数字,打印一系列行指示哪些数字是其他数字的除数。对于每个,打印出“X 除 Y”,其中 X <= Y,并且 X 和 Y 都在 2 和 100 之间。前几行将是: 2 除以 2 3 除以 3 2 除以 4 4 除以 4 5 除以 5等等。


到目前为止我有这个


x = 2

y = 2

while y <= 100:

      while y <= 100:

            if y % x == 0:

                print(x, 'divides', y)

                y += 1

            elif y % x != 0:

                y += 1

我不知道如何让它测试 x 和 y 的其他值

吃鸡游戏
浏览 113回答 3
3回答

隔江千里

这应该做一个工作。试一试,如果有什么不清楚的给我喊x = int(input("Give the range you want to check numbers in: "))for number in range(1,x):&nbsp; &nbsp; for value in range(1,number+1):&nbsp; &nbsp; &nbsp; &nbsp; if number % value == 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(number, " is divided by", value)输入“10”的输出:1&nbsp; is divided by 12&nbsp; is divided by 12&nbsp; is divided by 23&nbsp; is divided by 13&nbsp; is divided by 34&nbsp; is divided by 14&nbsp; is divided by 24&nbsp; is divided by 45&nbsp; is divided by 15&nbsp; is divided by 56&nbsp; is divided by 16&nbsp; is divided by 26&nbsp; is divided by 36&nbsp; is divided by 67&nbsp; is divided by 17&nbsp; is divided by 78&nbsp; is divided by 18&nbsp; is divided by 28&nbsp; is divided by 48&nbsp; is divided by 89&nbsp; is divided by 19&nbsp; is divided by 39&nbsp; is divided by 9

白衣染霜花

您可以在此处使用enumerate并在每个项目之前循环遍历索引将产生您想要获得的结果x = [*range(2, 101)]for idx, item in enumerate(x):&nbsp; &nbsp; for i in x[:idx +1]:&nbsp; &nbsp; &nbsp; &nbsp; if not item % i:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('{} divides {}'.format(i, item))2 divides 23 divides 32 divides 44 divides 45 divides 52 divides 63 divides 66 divides 6...99 divides 992 divides 1004 divides 1005 divides 10010 divides 10020 divides 10025 divides 10050 divides 100100 divides 100
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python