在特殊情况下使用print命令

2024-04-29 16:53:23 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有办法在print之后使用if not突击队?如果有,如何使用

def Move(self, tile, startx, starty): 
    if self.board[xstart][ystart] != ' ' or not self.isOnBoard(xstart, ystart):
        print('The coordinates is not on the board')
        return False

Tags: orselfboardmoveifdefnot突击队
2条回答

我不知道机上功能内部发生了什么;也许是类似的

但是当你检查的时候

   if self.board[xstart][ystart] != ' ' 

当xstart和/或ystart超出有效范围(我假设这就是您所说的“不在板上”的意思)时,您将得到一个错误,因为您试图访问一些不存在的内容

在使用xstart和ystart的值之前,需要检查它们是否在有效范围内

如果这就是你在isOnBoard中所做的,你应该先检查它,然后检查板上那个点的内容

如果你给出了isOnBoard的代码,如果它不能正常工作,我们也许可以帮助你

The problem is that still when I input a coordinat that is outside of the gameboard, it only gives me Errors when I want the function to break and print that that move is not valid PythonGirl

鉴于此。似乎你想测试startx,starty是否存在于板上(我假设是一个预定义的板)的有效性。在这种情况下,重新安排测试不会解决它。当您第一次测试这些值是否存在时(通过self.board[xstart][ystart]),并没有实际使用isOnBoard函数测试它们是否在板上

def Move(self, tile, startx, starty):
if not self.isOnBoard(xstart, ystart) or self.board[xstart][ystart] == ' ':
    print('The coordinates is not on the board')
    return False

相关问题 更多 >