Click here to Skip to main content
15,887,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
c = (3,3)
d = (3,5)

bondy = [4]
bondx = [3]

def adjacentnode(nodea ,nodeb):
    if nodea[0] == nodeb[0] and nodea[1] == nodeb[1]+2 and nodea[1]+1 in bondy:
        adjacent = True
    elif nodea[0] == nodeb[0] and nodea[1] == nodeb[1]-2 and nodea[1]-1 in bondy:
        adjacent = True
    elif nodea[1] == nodeb[1] and nodea[0] == nodeb[0]+2 and nodeb[0]+1 in bondx:
        adjacent = True
    elif nodea[1] == nodeb[1] and nodea[0] == nodeb[0]-2 and nodeb[0]-1 in bondx:
        adjacent = True
    else:
        adjacent = False
    return adjacent


print (adjacentnode((c),(d)))


What I have tried:

surely c and d are adjacent and should return true
Posted
Updated 2-May-19 7:58am

First change your code to print partial results of the conditions
Python
def adjacentnode(nodea ,nodeb):
    print ("First consition")
    print (nodea[0] == nodeb[0])
    print (nodea[1] == nodeb[1]+2)
    print (nodea[1]+1 in bondy)
    ... # and so on for other conditions
    if nodea[0] == nodeb[0] and nodea[1] == nodeb[1]+2 and nodea[1]+1 in bondy:
        adjacent = True
    elif nodea[0] == nodeb[0] and nodea[1] == nodeb[1]-2 and nodea[1]-1 in bondy:
        adjacent = True
    elif nodea[1] == nodeb[1] and nodea[0] == nodeb[0]+2 and nodeb[0]+1 in bondx:
        adjacent = True
    elif nodea[1] == nodeb[1] and nodea[0] == nodeb[0]-2 and nodeb[0]-1 in bondx:
        adjacent = True
    else:
        adjacent = False
    return adjacent

and it will show you where code fails.
Quote:
surely c and d are adjacent and should return true

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Add the following two lines at the beginning of your function, and you will immediately see what is wrong:
Python
print("nodea[0]:", nodea[0], "nodeb[0]:", nodeb[0])
print("nodea[1]:", nodea[1], "nodeb[1]+2:", nodeb[1]+2)
 
Share this answer
 
Comments
Member 14194093 2-May-19 14:40pm    
does That does not make a difference as it will go to the if block with -2
Member 14194093 2-May-19 14:43pm    
def adjacentnode(nodea ,nodeb):
if nodea[0] == nodeb[0] and nodea[1] == nodeb[1]+2:
adjacent = True
elif nodea[0] == nodeb[0] and nodea[1] == nodeb[1]-2:
adjacent = True
elif nodea[1] == nodeb[1] and nodea[0] == nodeb[0]+2 :
adjacent = True
elif nodea[1] == nodeb[1] and nodea[0] == nodeb[0]-2:
adjacent = True
else:
adjacent = False
return adjacent

only when nodea[1]+1 in bondy: statement is added it stops working
Richard MacCutchan 3-May-19 3:13am    
But your code has nodea[1]-1 in bondy which is false. I have no idea what actual problem you are trying to solve here but unless your rules are specific to the values in the two lists, you will always have problems.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900