2017-09-19 68 views
-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 
+3

你永遠不會改變'words'的值,因此總是小於50k。你也應該小心if-test周圍的縮進。 – MatsLindh

+0

爲什麼你需要'keep_going'?一旦你有超過'max_comments',你是不是想擺脫循環?如果是這樣,你不需要'keep_going'。 –

+3

除了永不改變單詞的值,您也不會改變number_of_comments的值 – Legman

回答

-1

你必須檢查是否total_words < = 50000,不言< = 500000你不?

+1

您應該將問題作爲對原始帖子的評論;)這裏有一個更大的問題,那就是total_words在while循環之前從未初始化,所以這段代碼實際上是崩潰的。他們的實際代碼必須是不同的東西。 –