我如何检查每个数字而不是一个值?
How do I check each number instead just the one value?
除数:
对于从 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 的其他值
这是您的代码的更正版本,较小的 y
值最多为 6。
您可以将其扩展到 100。
说明:第一个 while 循环检查 y
值。对于每个 y
值,您使用遍历 x
的第二个 while 循环检查其除数。您在内部 while
循环中将 x
更新为 1,并在外部 while
循环中将 y
更新为 1。如果有不清楚的地方,请在下方评论。
您的代码有问题:您使用了两个 while
循环只是为了 y
,其中一个是多余的。此外,正如您在问题中明确指出的那样,您并没有增加 x
。您的 elif
也不是必需的,因为在这两种情况下您都在递增 y
。
y = 2
while y <= 6: # Replace 6 by 100 here
x = 2 # Reset x to 2 for every y value because you count divisors from 2
while x <= y:
if y % x == 0:
print(x, 'divides', y)
x += 1
y += 1
输出
2 divides 2
3 divides 3
2 divides 4
4 divides 4
5 divides 5
2 divides 6
3 divides 6
6 divides 6
这应该是一项工作。试一试,有什么不明白的地方给我留言
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
您可以在此处使用 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
除数: 对于从 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 的其他值
这是您的代码的更正版本,较小的 y
值最多为 6。
您可以将其扩展到 100。
说明:第一个 while 循环检查 y
值。对于每个 y
值,您使用遍历 x
的第二个 while 循环检查其除数。您在内部 while
循环中将 x
更新为 1,并在外部 while
循环中将 y
更新为 1。如果有不清楚的地方,请在下方评论。
您的代码有问题:您使用了两个 while
循环只是为了 y
,其中一个是多余的。此外,正如您在问题中明确指出的那样,您并没有增加 x
。您的 elif
也不是必需的,因为在这两种情况下您都在递增 y
。
y = 2
while y <= 6: # Replace 6 by 100 here
x = 2 # Reset x to 2 for every y value because you count divisors from 2
while x <= y:
if y % x == 0:
print(x, 'divides', y)
x += 1
y += 1
输出
2 divides 2
3 divides 3
2 divides 4
4 divides 4
5 divides 5
2 divides 6
3 divides 6
6 divides 6
这应该是一项工作。试一试,有什么不明白的地方给我留言
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
您可以在此处使用 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