python - 我的家庭作业有问题。这是关于停止循环

标签 python for-loop while-loop break

我正在做作业。而且我不知道如何执行此解决方案。

我试图在 for 语句下使用中断但没有任何返回。
问题是“完成以下程序,使循环在找到大于 1000 且可被 33 和 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

我不知道我应该在哪一行中放置 break (或者我不必使用它?)或者我应该创建一个调用列表最小数量的函数?

我很抱歉我的语言以及我的编程技能有多愚蠢
我会接受每一条评论。谢谢

最佳答案

您已经拥有 while True:循环,你不需要内部 for循环搜索您的号码,只需不断递增 nwhile循环而不是添加新计数器,当找到您要查找的数字时,无限 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

关于python - 我的家庭作业有问题。这是关于停止循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57859209/

相关文章:

python - 试图掌握 clang\cindex.py 中的 CachedPropery

java - 为什么在下面的线程代码中,for 循环比 if 迭代更多的素数

xml - 如何在 Qweb 中制作具有特定循环次数的 "for-loop"?

python - 如何减少语句中的条件数量?

JavaScript Prime 检查器不工作

python - 如何在 django 管理站点中使用 django-select2 小部件?

python - 交错集群的算法

javascript - 如何抓取将所有交互都作为回发进行的 ASP.NET 站点?

javascript - 是否可以将 while 循环告诉 'keep looping until this condition is not met, then execute one more time' ?

javascript - 尝试查找标题是否包含单词然后隐藏该父元素