有条件的错误是什么?

时间:2015-11-29 00:30:39

标签: python conditional

我有这段代码:

    listOfColumns = ["A", "B", "C", "D", "E", "F", "G", "H"]
    validChoice = False
    if color1 == "White":
        piece = raw_input(self.Player1["Name"] + ", pick a piece to move. Enter the coordinates (ex. A3, D4, etc.)." + "\n")
        while validChoice == False:
            column = self.position_to_xCoor(piece)
            row = self.position_to_yCoor(piece)
            piece1 = self.Chessboard.Matrix[row][column].Piece
            print piece1.Color
            print color1
            if str(piece[0]) != any(self.listOfColumns) or int(piece[1]) > 8 or piece1.Color != color1:
                piece = raw_input("That is not a valid choice. Pick again." + "\n")
            else:
                validChoice = True

其中Chessboard是另一个具有属性Piece的类的对象,该属性具有属性颜色。用户意味着输入坐标(即A2,B3,D7)。但为什么我一直得到这个结果呢?我以为我的所有条件都得到了满足。

White
White
That is not a valid choice. Pick again.

1 个答案:

答案 0 :(得分:1)

这不是any()的工作方式。 any()的结果是TrueFalse,而str()的某些内容会返回字符串,而不是布尔值。您需要in运算符:

if str(piece[0]) not in self.listOfColumns or...