2014-05-14 62 views
1

我想要做的是循環中的元素,尋找「P」,然後檢查是否有5「 P「連續(向下,可以這麼說)。請參閱代碼中的註釋以獲得進一步解釋仍然,inRow給我輸出15.循環列表(嵌套for循環)不工作,因爲我想它

提示:五個「P」不應該是固定的,而是由玩家放置,但對於這個例子,我只是放置了固定的。

board = [['E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E'], 
     ['E', 'E', 'P', 'E', 'E', 'E', 'E', 'E', 'E', 'E'], 
     ['E', 'E', 'P', 'E', 'E', 'E', 'E', 'E', 'E', 'E'], 
     ['E', 'E', 'P', 'E', 'E', 'E', 'E', 'E', 'E', 'E'], 
     ['E', 'E', 'P', 'E', 'E', 'E', 'E', 'E', 'E', 'E'], 
     ['E', 'E', 'P', 'E', 'E', 'E', 'E', 'E', 'E', 'E'], 
     ['E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E'], 
     ['E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E'], 
     ['E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E'], 
     ['E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E']] 
# has 10 rows, with 10 elements in each 

inRow=0 

for y in range(10): # Loops over each row (y-index) 
    x=0 # x-index resets each new y-index loop (start on the first element (0) of each row) 
    for pos in board[y]: # pos loops over all elements in current row 
     if pos is "P": # checks if pos is "P" 
      for i in range(5): # loops over 0, 1, 2, 3, 4 
       if board[y+i][x] is "P": # when i=0, board[y+i][x] is ought to be where we find the first "P", then check if the following rows (we add +1 to y for each loop) is also "P" 
        inRow += 1 # Counter to see if we got 5 in a row 
      break 
     x+=1 

print(inRow) 
+0

作者希望'inRow'是'5'而不是'15'。 – huu

+3

不要在字符串中使用'is'。 '是'測試相同的對象,而不是相等的字符串。 – Daniel

+0

你是否需要將它製作成嵌套循環?如果是這樣,一個建議是首先在列上循環,然後在行上循環。 – pekapa

回答

0

的問題是,當你打破,你不結束兩個循環 - 只有第二個。

因此,當您的代碼找到第一個P時,它將增加inRow,直到它等於5,然後中斷。然後你的代碼移到第二行,到第二行P,然後遞增inRow,直到它等於9 - 5 + 4 = 9

雖然你可以調整你的代碼,以完全避免這個問題,你也可以換行代碼的功能和簡單的返回,而不是打破:

board = [['E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E'], 
     ['E', 'E', 'P', 'E', 'E', 'E', 'E', 'E', 'E', 'E'], 
     ['E', 'E', 'P', 'E', 'E', 'E', 'E', 'E', 'E', 'E'], 
     ['E', 'E', 'P', 'E', 'E', 'E', 'E', 'E', 'E', 'E'], 
     ['E', 'E', 'P', 'E', 'E', 'E', 'E', 'E', 'E', 'E'], 
     ['E', 'E', 'P', 'E', 'E', 'E', 'E', 'E', 'E', 'E'], 
     ['E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E'], 
     ['E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E'], 
     ['E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E'], 
     ['E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E']] 

def find(board): 
    inRow=0 
    for y in range(10): 
     x = 0 
     for pos in board[y]: 
      if pos == "P": 
       for i in range(5): 
        if board[y+i][x] == "P": 
         inRow += 1 
       return inRow 
      x += 1 
    return inRow 

print(find(board)) 
+0

我不認爲這是嚴格正確的,儘管它會爲作者提供正確的輸出以解決他們遇到的具體問題。如果您在第一個右側放置第二組「P」,則輸出將是「5」而不是預期的「10」。 – huu

+0

儘管我沒有使用你的建議來解決它(因爲我做的方式在整個項目中使用時更合適(而不僅僅是我粘貼在這裏的小例子)),你幫我明白了什麼是錯的(「不打破第一個for-loop「部分),所以非常感謝! – pottus

1

你甚至當沒有5 「P」 S連續添加到inRow。想想這個:

第一次你在第二行打「P」時,你倒數了,你得到了5。當您下一次在外部for循環中擊中「P」時,您倒數並且您得到4。現在你已經計算了9「P」。繼續這個,你會得到5 + 4 + 3 + 2 + 1 = 15「P」。

解決方法是使用中間計數器,並且只將該中間計數添加到inRow,如果該計數器是5

要算錯了,以後還可以for pos in board[y]:

counter = 0 

然後右鍵聲明以代替inRow += 1計數器,您使用counter += 1

最後,你休息語句之前,做一次檢查:

if counter == 5: 
    inRow += counter 
+0

如果你的船上有5個以上的「P」,那麼這非常適合,並且可以搜索所有5行的船。但是,我一次只會有5個「P」(我應該提到它,對不起!),因此另一個帖子被標記爲答案。 – pottus

0

你必須設置標誌,如果沒有5個「P 'in row then then check if flag is true then then in Row

inRow=0 

for y in range(10): 
    x=0 
    for pos in board[y]: 
     if pos is "P": 
      flag=True 

      for i in range(5): 
       if not board[y+i][x] is "P": 
        flag=False 
      if flag: 
       inRow+=1 
      break 
     x+=1 

print(inRow) 
+0

這將返回'1'。作者期待'5'。 – huu