亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我的作業有問題。這是關于停止循環

我的作業有問題。這是關于停止循環

呼喚遠方 2022-05-24 16:57:28
我正在做作業。而且我不知道如何做這個解決方案。我曾嘗試在 for 語句下使用 break,但沒有任何回報。問題是“完成以下程序,以便循環在找到大于 1000 且可被 33 和 273 整除的最小正整數時停止?!边@是我嘗試過的代碼n = 1001 #This one is requiredwhile 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我不知道我應該在哪些行中放置中斷(或者我不必使用它?)或者我應該創建一個調用列表最小數量的函數?我很抱歉我的語言以及我的編程技能有多愚蠢,我會接受每一條評論。謝謝
查看完整描述

3 回答

?
慕容森

TA貢獻1853條經驗 獲得超18個贊

您已經有一個while True:循環,您不需要內部for循環來搜索您的號碼,只需n在while循環中不斷增加而不是添加新的計數器,當找到您要查找的號碼時,無限while True:循環將停止( using 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


查看完整回答
反對 回復 2022-05-24
?
DIEA

TA貢獻1820條經驗 獲得超2個贊

謝謝你說這是家庭作業!比起僅僅給出答案,更詳細地解釋事情會更好。


有幾點需要解釋:


1) n%33 是 n 除以 33 的余數。所以 66%33 為 0,67%33 為 1。


2) For 循環通常是當您需要在定義的范圍內循環時(并非總是如此,但通常如此)。例如“將前 100 個整數相加”。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


查看完整回答
反對 回復 2022-05-24
?
交互式愛情

TA貢獻1712條經驗 獲得超3個贊

好吧,您仍然可以使用for循環,只要上限至少與最大可能結果一樣高。結果將在 中i,而不是在 n 中,for循環就足夠了,而不是額外的while循環。當for除以 33 和 237 時的余數為零(即它們都是因數)時,循環將中斷。


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}") 


查看完整回答
反對 回復 2022-05-24
  • 3 回答
  • 0 關注
  • 137 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號