2017-09-19 50 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循环之前从未初始化,所以这段代码实际上是崩溃的。他们的实际代码必须是不同的东西。 –