我正在做作业。而且我不知道如何执行此解决方案。
我试图在 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
循环搜索您的号码,只需不断递增 n
在 while
循环而不是添加新计数器,当找到您要查找的数字时,无限 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/