我的作业有点问题。这是关于停止循环
I have some problem with my homework. It's about stop the loops
我正在做作业。而且我找不到如何解决这个问题。
我曾尝试在 for 语句下使用 break,但什么也没用 return。
问题是"Complete the following program so that the loop stops when it has found the smallest positive integer greater than 1000 that is divisible by both 33 and 273."
这是我尝试过的代码
n = 1001 #This one is required
while True: #This one too
for i in range(n,___): # I don't know what should i put in the blank
if i%33 == 0 and i%273 == 0: # I really confused about this line
break # Should i break it now?, or in the other lines?
print(f"The value of n is {n}") #This one is also required
我不知道我应该在哪几行打断(或者我不必使用它?)或者我应该创建一个函数来调用列表的最小数量?
我对我的语言和我的编程技巧多么愚蠢感到抱歉
我会接受每一条评论。谢谢
for 循环在这里帮不了你,因为你不知道什么时候结束循环。当您想要循环的范围已知时,通常使用 for 循环。
而是执行以下操作:
在开始你的 while: True
循环之前:将 i
设置为 0,
然后i
每次加1循环
此外,不要忘记在 i>1000
!
时停止循环
您已经有一个 while True:
循环,您不需要内部 for
循环来搜索您的号码,只需在 while
中继续递增 n
循环而不是添加一个新的计数器,当找到你要查找的数字时,无限 while True:
循环将停止(使用 break
),因此你的打印语句将被执行:
n = 1001 # start at 1001
while True: # start infinite loop
if n % 33 == 0 and n % 273 == 0: # if `n` found
break # exit the loop
n += 1 # else, increment `n` and repeat
print(f"The value of n is {n}") # done, print the result
输出:
The value of n is 3003
感谢您说这是作业!更详细地解释事情比仅仅给出答案更好。
有几点需要说明:
1)n%33是n除以33的余数,所以66%33是0,67%33是1。
2)for循环一般是当你需要在定义的范围内循环时(不总是,但通常)。例如。 "add up the first 100 integers"。 while 循环在这里更有意义。它肯定会终止,因为在某个时候你会达到 33 * 237.
3) if i%33 == 0 and i%237 == 0: 意思是当数字可以被 37 和 237 均分(无余数)时我们想做一些事情。
n=1001
while True:
if n%33==0 and n%237==0:
print(n)
break
n+=1
你仍然可以使用 for
循环,只要上限至少与最大可能结果一样高。结果将在 i
中,而不是在 n 中,并且 for
循环就足够了,而不是额外的 while
循环。当除以 33 和 237 的余数为零(即它们都是因数)时,for
循环将中断。
n = 1001 #This one is required
for i in range(n, 33 * 237 + 1): # I don't know what should i put in the blank
if i % 33 == 0 and i % 237 == 0: # I really confused about this line
break #
print(f"The value of i is {i}") #This one is also required
您也可以使用 while 循环并对条件使用相同的逻辑。在这种情况下,我们测试至少一个不是因子并继续循环,直到 33 和 237 均被 i.
整除
n = 1001 #This one is required
i = n
while i % 33 or i % 237:
i += 1
print(f"The value of i is {i}")
我正在做作业。而且我找不到如何解决这个问题。
我曾尝试在 for 语句下使用 break,但什么也没用 return。 问题是"Complete the following program so that the loop stops when it has found the smallest positive integer greater than 1000 that is divisible by both 33 and 273."
这是我尝试过的代码
n = 1001 #This one is required
while True: #This one too
for i in range(n,___): # I don't know what should i put in the blank
if i%33 == 0 and i%273 == 0: # I really confused about this line
break # Should i break it now?, or in the other lines?
print(f"The value of n is {n}") #This one is also required
我不知道我应该在哪几行打断(或者我不必使用它?)或者我应该创建一个函数来调用列表的最小数量?
我对我的语言和我的编程技巧多么愚蠢感到抱歉 我会接受每一条评论。谢谢
for 循环在这里帮不了你,因为你不知道什么时候结束循环。当您想要循环的范围已知时,通常使用 for 循环。
而是执行以下操作:
在开始你的 while: True
循环之前:将 i
设置为 0,
然后i
每次加1循环
此外,不要忘记在 i>1000
!
您已经有一个 while True:
循环,您不需要内部 for
循环来搜索您的号码,只需在 while
中继续递增 n
循环而不是添加一个新的计数器,当找到你要查找的数字时,无限 while True:
循环将停止(使用 break
),因此你的打印语句将被执行:
n = 1001 # start at 1001
while True: # start infinite loop
if n % 33 == 0 and n % 273 == 0: # if `n` found
break # exit the loop
n += 1 # else, increment `n` and repeat
print(f"The value of n is {n}") # done, print the result
输出:
The value of n is 3003
感谢您说这是作业!更详细地解释事情比仅仅给出答案更好。
有几点需要说明:
1)n%33是n除以33的余数,所以66%33是0,67%33是1。
2)for循环一般是当你需要在定义的范围内循环时(不总是,但通常)。例如。 "add up the first 100 integers"。 while 循环在这里更有意义。它肯定会终止,因为在某个时候你会达到 33 * 237.
3) if i%33 == 0 and i%237 == 0: 意思是当数字可以被 37 和 237 均分(无余数)时我们想做一些事情。
n=1001
while True:
if n%33==0 and n%237==0:
print(n)
break
n+=1
你仍然可以使用 for
循环,只要上限至少与最大可能结果一样高。结果将在 i
中,而不是在 n 中,并且 for
循环就足够了,而不是额外的 while
循环。当除以 33 和 237 的余数为零(即它们都是因数)时,for
循环将中断。
n = 1001 #This one is required
for i in range(n, 33 * 237 + 1): # I don't know what should i put in the blank
if i % 33 == 0 and i % 237 == 0: # I really confused about this line
break #
print(f"The value of i is {i}") #This one is also required
您也可以使用 while 循环并对条件使用相同的逻辑。在这种情况下,我们测试至少一个不是因子并继续循环,直到 33 和 237 均被 i.
整除n = 1001 #This one is required
i = n
while i % 33 or i % 237:
i += 1
print(f"The value of i is {i}")