簡體   English   中英

如何停止 python 無限循環?

[英]How to stop python infinite loop?

import random   
n=int(input())  

array=[]     
for i in range(0,n): 
    flag=True    
    while flag:
           print("flag=",flag)
           a=[]
           for j in range(0,n):
               if i!=j:
                  w= random.randint(0,1)
                  print("random=",w)
                  if w==1 and i<j:
                     v=random.randint(1,10)
                     a.append(v)
                  else:
                      a.append(0)
               elif i==j:
                    a.append(0)
           print(a)
           if a.count(0) !=n:
              print("a.count(0)=",a.count(0))
              flag=False
              print("flag value=",flag)
      array.append(a)
print(array) 

在這個程序中,如果零的數量不等於 n,它應該打破循環並將 append 它到矩陣但是這個循環運行無限次,標志的值被分配為假,但標志值仍然是真。這程序是為每一行生成至少一個隨機 integer ,所以我正在檢查零是否不等於列數打破循環。有什么辦法嗎?

所以請告訴我當零計數不等於輸入n時如何停止這個循環?

好的,首先,讓我們修復縮進和 PEP8 問題並添加一些類型注釋,這樣我們就可以看到發生了什么。 (添加類型注釋幫助我修復了縮進,因為嘗試自動修復它會導致語法錯誤,我不得不返回並更正 go。)我剛剛刪除了Graph的東西,因為無論如何你都沒有在這段代碼中使用它而且我沒有不知道那個進口是從哪里來的。

import random
from typing import List

n = int(input())
array: List[List[int]] = []
for i in range(0, n):
    flag = True
    while flag:
        print("flag=", flag)
        a: List[int] = []
        for j in range(0, n):
            if i != j:
                w = random.randint(0, 1)
                print("random=", w)
                if w == 1 and i < j:
                    v = random.randint(1, 10)
                    a.append(v)
                else:
                    a.append(0)
            elif i == j:
                a.append(0)
        print(a)
        if a.count(0) != n:
            print("a.count(0)=", a.count(0))
            flag = False
            print("flag value=", flag)
    array.append(a)
print(array)

現在讓我們看一下output:

2
flag= True
random= 0
[0, 0]
flag= True
random= 1
[0, 6]
a.count(0)= 1
flag value= False
flag= True
random= 0
[0, 0]
flag= True
random= 1
[0, 0]
flag= True
random= 0
[0, 0]
flag= True
random= 0
[0, 0]
flag= True

因此,我們第一次通過for i循環 (i=0) 打破了內部while循環,但我們在for i的每次迭代中返回並重新啟動它,而在第二次 (i=1) 中,我們總是以a == [0] * n結束。 當我嘗試使用更高的n值時,似乎我們總是以這個 state 結束。

那么,我們在那個for j循環中做了什么,當我們在最后一個i時,總是導致一個數組全為零? 好吧,這一切都歸結為這個塊:

                w = random.randint(0, 1)
                print("random=", w)
                if w == 1 and i < j:
                    v = random.randint(1, 10)
                    a.append(v)
                else:
                    a.append(0)

ij都是相同range(n)上的迭代。 i處於最后一次迭代時(即i == n - 1 ),則永遠不可能有i < j因為i已經處於它們中的任何一個可以具有的最大值。 這意味着無論w的值如何,我們都將始終執行a.append(0) ,因此無論我們通過該 while 循環多少次 go ,都將始終以全零數組結束。

在最后的i迭代中, flag保持為True ,因為a.count(0) == n始終為真,因此在for i循環的最后一次內的while flag循環將永遠不會終止。

向我們自己證明這一點的一種方法是簡單地添加一個assert ,如下所示:

                if w == 1 and i < j:
                    # This block is the only thing that can ever set flag to False!
                    assert i < n - 1, "We never get here on the last loop!"
                    v = random.randint(1, 10)
                    a.append(v)

並觀察到這永遠不會導致AssertionError

Python 不會進行無限循環,因為它對此有保護措施。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM