HackerRank 'Bot Saves Princess 2' I submitted the following Python 3 code for this HackerRank challenge. Given an n × n grid with an m and a p in random cells of the grid, it prints the next step in a path that moves the m to the p. I'd like any feedback on this.

def nextMove(n,r,c,grid):
    #First, find the princess
    for i in range(len(grid)):
        for j in range(len(grid[0])):
            if grid[i][j] == "p":
                a = i
                b = j    
    # i = row, j = column
    nm = ("UP" if (r-a) &gt; 0 else "DOWN")
    if r-a == 0:
        nm = ("LEFT" if (c-b) &gt; 0 else "RIGHT")
    return nm

n = int(input())
r,c = [int(i) for i in input().strip().split()]
grid = []
for i in range(0, n):
    grid.append(input())

print(nextMove(n,r,c,grid))
Parsing input

input().strip().split() can be reduced to input().split() as split without parameters has extra logic to remove empty strings in the result. It feels also more idiomatic to unpack the two integers using r, c = map(int, input().split())

Parsing princess position

First of, you could create the grid using a list-comprehension rather than append:

grid = [input() for _ in range(n)]


Note that I dropped 0 from the range parameter as it the default starting point.

But you don't even need to build the whole grid since you’re only interested in knowing the princess position. You could search for it while parsing the input:

def princess_position(grid_size):
    for row in range(grid_size):
        try:
            return row, input().index('p')
        except ValueError:
            # Princess not on this line
            pass
    # Princess not in grid
    return -1, -1

grid_size = int(input())
bot_row, bot_column = map(int, input().split())
princess_row, princess_column = princess_position(grid_size)
print(next_move(bot_row, bot_column, princess_row, princess_column))


Comparing positions

You don't need to put parenthesis around ternary operators. You can also simplify a - b &gt; 0 to a &gt; b; same for other comparisons. Last thing, you can perform less work by comparing a == r first and returning left or right before comparing for up and down:

def next_move(bot_row, bot_column, princess_row, princess_column):
    if bot_row == princess_row:
        return 'LEFT' if bot_column &gt; princess_column else 'RIGHT'
    return 'UP' if bot_row &lt; princess_row else 'DOWN'


Top-level code

It is better practice to wrap your top-level code under an if __name__ == '__main__' statement, as it allows you to import your file without executing the whole thing:

def next_move(bot_row, bot_column, princess_row, princess_column):
    if bot_row == princess_row:
        return 'LEFT' if bot_column &gt; princess_column else 'RIGHT'
    return 'UP' if bot_row &lt; princess_row else 'DOWN'

def princess_position(grid_size):
    for row in range(grid_size):
        try:
            column = input().index('p')
        except ValueError:
            # Princess not on this line
            pass
        else:
            return row, column
    # Princess not in grid
    return -1, -1

if __name__ == '__main__':
    grid_size = int(input())
    print(next_move(
        *map(int, input().split())
        *princess_position(grid_size))


I also changed the way to use unpacking to simplify variable managements.