2015-06-05 30 views
0

我有一段代碼,其中有幾個循環。通過檢查條件(使用if語句)我想從除最後一個之外的所有循環中出來。有沒有辦法做到這一點,而無需修改我的代碼,因爲我沒有多少時間來提交它。打破了for循環的混合物,如果條件

for file in dirs: 
    .... 
    .... 
    #Opens a file in the directory 
    with open(path, 'rU') as csvfile: 

     .... 
     .... 
     .... 
     #Iterates over every row in the File 
     for row in csvreader: 
      ... 
      #If the data is insufficient, the next row will be iterated. 
      if len(Parameters)>15:      
      #If this condition is not satisfied then I need to go to the next iteration of the first 'for' loop 
      #Without Calculating the Average_Slip 
      .... 
      .... 
      Calculation_of_Slip() 
      #Because when all the rows are done iterating, Average_Slip encounters an error as the input values depend on the Slip. 
      #Instead I need to go to the next file in the directory 
    Average_slip(Slip_3MW,Slip_7MW,Slip_9MW,Counter_1, Counter_2, Counter_3, Counter_4) 

我確定我錯過了一些非常簡單的東西,但任何人都可以幫助我。

回答

2

基於an answer to a previous similar question:您可以將您的代碼重構爲函數的嵌套循環,並將其與return分開。

這也會增加可讀性,因爲如果層次太多,嵌套流控制可能會變得混亂。