Ach*_*run 7 python for-loop break while-loop
我正在做作业。而且我找不到如何执行此解决方案。
我已经尝试过在声明中使用中断,但没有任何回报。问题是“完成以下程序,以使循环在找到大于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
Run Code Online (Sandbox Code Playgroud)
我不知道我应该在哪些行中打断(或者我不必使用它?),或者我应该创建一个调用列表最小数量的函数?
我为自己的语言以及接受每条评论而对自己的编程技能有多愚蠢感到抱歉。谢谢
你已经有了一个while True:循环,你不需要内for循环来搜索你的数字,只需n在while循环中不断递增而不是添加新的计数器,当找到你要寻找的数字时,无限while True:循环就会停止(使用break),因此您的 print 语句将被执行:
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
Run Code Online (Sandbox Code Playgroud)
输出:
The value of n is 3003
Run Code Online (Sandbox Code Playgroud)