为什么它在子例程中返回false [英] Why does it return false in the subroutine

查看:60
本文介绍了为什么它在子例程中返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 c =(3,3)
d =(3,5)

bondy = [4]
bondx = [3]

def adjacentnode(nodea,nodeb):
如果nodea [0] == nodeb [0]和nodea [1] == nodeb [1] +2和nodea [1] +1 in bondy:
adjacent = True
elif nodea [0] == nodeb [0]和nodea [1] == nodeb [1] -2和nodea [1] -1 in bondy:
adjacent = True
elif nodea [1] == nodeb [1]和nodea [0] == nodeb [0] +2和nodeb [0] +1 in bondx:
adjacent = True
elif nodea [1] == nodeb [1]和nodea [0] == nodeb [0] -2和nodex [0] -1 in bondx:
adjacent = True
else:
adjacent = False
返回邻近


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





我尝试了什么:



肯定c和d相邻,应该返回true

解决方案

首先更改你的代码以打印条件的部分结果

  def  adjacentnode(nodea,nodeb):
print 第一个信息
print (nodea [ 0 ] == nodeb [ 0 ])
print (nodea [ 1 ] == nodeb [ 1 ] + 2
print (nodea [ 1 ] + 1 in bondy)
... 等等其他条件
如果 nodea [ 0 ] = = nodeb [ 0 ] nodea [ 1 ] == nodeb [ 1 ] + 2 nodea [ 1 ] + 1 in bondy:
adjacent = True
elif nodea [ 0 ] = = nodeb [ 0 ] nodea [ 1 ] == nodeb [ 1 ] - 2 nodea [ 1 ] - 1 bondy:
adjacent = True
elif nodea [ 1 ] == nodeb [ 1 ] nodea [< span class =code-digit> 0 ] == node b [ 0 ] + 2 nodeb 0 ] + 1 bondx:
adjacent = True
elif nodea [ 1 ] == nodeb [ 1 ] nodea [ 0 ] == nodeb [ 0 ] - 2 nodeb [ 0 ] - 1 中的code-keyword>:
adjacent = True
else
adjacent = 错误
返回相邻



和它wi ll告诉你代码失败的地方。

引用:

肯定c和d是相邻的,应该返回true



您的代码没有按照您的预期行事,或者您不明白为什么!



有一个几乎通用的解决方案:在调试器上逐步运行代码,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它不知道你的代码应该做什么,它没有找到错误,它只是通过向你展示发生了什么来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]


掌握调试Visual Studio 2010 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



27.3。 pdb - Python调试器 - Python 3.6.1文档 [ ^ ]

使用Python进行调试Python征服宇宙 [ ^ ]

pdb - 交互式调试器 - 本周的Python模块 [ ^ ]



调试器只是向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。


在函数的开头添加以下两行,您将马上就看错了:

  print   nodea [0]:,nodea [ 0 ],  nodeb [0]:,nodeb [ 0 ])
print nodea [1]:,nodea [ 1 ], nodeb [1] +2:,nodeb [ 1 ] + 2


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

解决方案

First change your code to print partial results of the conditions

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.


Add the following two lines at the beginning of your function, and you will immediately see what is wrong:

print("nodea[0]:", nodea[0], "nodeb[0]:", nodeb[0])
print("nodea[1]:", nodea[1], "nodeb[1]+2:", nodeb[1]+2)


这篇关于为什么它在子例程中返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆