Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Why is it wrong at one case? (python)

+3 votes
asked Dec 13, 2021 by Артур (150 points)
The goal is to tell whether can the king go to the cell or not. 
n= abs (a-c)
m= abs (b-d)
if n==m==1:
elif a==c and b+1==d or d+1==b:
elif b==d and a+1==c or c+1==a:

1 Answer

+1 vote
answered Dec 17, 2021 by Peter Minarik (55,660 points)
edited Dec 17, 2021 by Peter Minarik

I don't see what the problem would be with your program. It seems to me it works fine. Could you provide an input where it does not work as expected?

Also, it is a good idea to have a proper problem description. E.g. This is a chess problem. We would like to see if the king chess piece can move from one cell to another. Both coordinates of the start and end cells are indexed by numbers.

I've changed your code a bit, made the user input more talkative and changed the variable names to more meaningful ones. Added some comments and simplified the check if the destination (end) cell is right next to the king's current location, i.e. their relative distance (offset) from one another is no more than 1 cell in either direction:

# User input
startX = int(input('startX: '))
startY = int(input('startY: '))
endX = int(input('endX: '))
endY = int(input('endY: '))
# Decision making
offsetX = abs(startX - endX)
offsetY = abs(startY - endY)
if (offsetX <= 1 and offsetY <= 1):

commented Jan 26 by Arhaan Mohammed (100 points)
a = int(input())

b = int(input())

c = int(input())

d = int(input())

n = abs (a-c)

m = abs (b-d)

if n == m == 1:
elif a == c and b + 1 == d or d + 1 == b:
elif b == d and a + 1 == c or c + 1 == a:

Make this a little readable.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.