Question

What I want this to do is loop over the elements in board, look for "P", then check if there are 5 "P"'s in a row (downwards, so to speak). See comments in code for further explanation. Still, inRow is giving me output 15.

Hint: the five "P"'s are not ought to be stationary but placed by the player but for this example, I just placed stationary ones.

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)
Was it helpful?

Solution 2

The problem is that when you break, you don't end both loops -- only the second one.

Consequently, when your code finds the first P, it increments inRow until it equals 5, then breaks. Then your code moves on to the second row, to the second P, and increments inRow until it equals 9 -- 5 + 4 = 9.

Although you can restructure your code to avoid this issue altogether, you can also wrap the code in a function and simply return instead of breaking:

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))

OTHER TIPS

You add to inRow even when there aren't 5 "P"s in a row. Think about this:

The first time you hit a "P" in the second row, you count down and you get 5. The next time you hit a "P" in the outer for loop, you count down and you get 4. Now you've counted 9 "P"s. Continue this and you'll get 5 + 4 + 3 + 2 + 1 = 15 "P"s.

The solution is to use an intermediate counter, and only add that intermediate count to inRow if that counter is 5.

To correctly count it, you can declare a counter right after for pos in board[y]:

counter = 0

Then, instead of inRow += 1, you use counter += 1.

Finally, right before your break statement, do a check:

if counter == 5:
    inRow += counter

you must set Flag if there are not 5 'P' in row then check if flag is true then increase inRow

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top