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

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

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}")
添加回答
舉報