简体   繁体   中英

What's wrong with my conditional?

I have this code:

    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

where Chessboard is an object of another class that has the attribute Piece, which has the attribute color. The user is meant to enter the coordinates (ie. A2, B3, D7). But why do I keep getting this result? I thought all of my conditionals were met.

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

That's not how any() works. The result of any() is True or False , while a str() of something will return a string, not a boolean. You need the in operator:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM