-1
我運行程序,但循環不停止時,單詞等於50,000。產量有時超過5萬。我不確定我做錯了什麼。python初學者 - 當條件滿足時循環不中斷
words = 0
unwanted_comments = 0
comments = 0
keep_going = True
while number_of_comments <= max_comments and keep_going == True:
num_per_comments = random.randint(1,1050)
if number_per_comments <= 1000:
words = number_per_comments + total
comments +=1
if words <= 50000:
keep_going = True
else:
keep_going = False
else:
unwanted_comments +=1
你永遠不會改變'words'的值,因此總是小於50k。你也應該小心if-test周圍的縮進。 – MatsLindh
爲什麼你需要'keep_going'?一旦你有超過'max_comments',你是不是想擺脫循環?如果是這樣,你不需要'keep_going'。 –
除了永不改變單詞的值,您也不會改變number_of_comments的值 – Legman