Python 为什么我的新手代码会额外增加?

Python 为什么我的新手代码会额外增加?,python,dictionary,reference,slice,increment,Python,Dictionary,Reference,Slice,Increment,这里是新的Python程序员,拥有相当丰富的c#经验。我试图用Python自动完成这个练习: 国际象棋字典验证器在本章中,我们使用了字典 值{'1h':'bking','6c':'wqueen','2g':'bbishop','5h':'bqueen', “3e”:“wking”}代表棋盘。编写一个名为 isValidChessBoard()接受字典参数并返回True 或False取决于电路板是否有效。有效的董事会将有 一个黑人国王和一个白人国王。每个玩家都可以 最多只能有16个棋子,最多8个棋

这里是新的Python程序员,拥有相当丰富的c#经验。我试图用Python自动完成这个练习:

国际象棋字典验证器在本章中,我们使用了字典 值{'1h':'bking','6c':'wqueen','2g':'bbishop','5h':'bqueen', “3e”:“wking”}代表棋盘。编写一个名为 isValidChessBoard()接受字典参数并返回True 或False取决于电路板是否有效。有效的董事会将有 一个黑人国王和一个白人国王。每个玩家都可以 最多只能有16个棋子,最多8个棋子,所有棋子必须是 在从'1a'到'8h'的有效空间上;也就是说,一件物品不可能在太空中 “9z”。工件名称以“w”或“b”开头表示 白色或黑色,后跟“兵”、“骑士”、“主教”、“车”, “女王”或“国王”。此函数应检测何时出现错误 导致棋盘不正确

我目前的解决办法如下:

testBoard00 = {'1h': 'bking', '6c': 'wqueen', '2g': 'bbishop', '5h': 'bqueen', '3e': 'wking'}

testBoard01 = {'1a': 'brook', '1b': 'bknight', '1c': 'bbishop', '1d': 'bqueen', '1e': 'bking', '1f' : 'bbishop', '1g' : 'bknight', '1h' : 'brook',
               '2a': 'bpawn', '2b' : 'bpawn', '2c': 'bpawn', '2d' : 'bpawn', '2e': 'bpawn', '2f' : 'bpawn', '2g': 'bpawn', '2h' : 'bpawn',
               '8a': 'wrook', '8b': 'wknight', '8c': 'wbishop', '8d': 'wqueen', '8e': 'wking', '8f' : 'wbishop', '8g' : 'wknight', '8h' : 'wrook',
               '7a': 'bpawn', '7b' : 'bpawn', '7c': 'bpawn', '7d' : 'bpawn', '7e': 'bpawn', '7f' : 'bpawn', '7g': 'bpawn', '7h' : 'bpawn',}

testBoard02 = {'1a': 'brook', '1b': 'bknight', '1c': 'bbishop', '1d': 'bqueen', '1e': 'bking', '1f' : 'bbishop', '1g' : 'bknight', '1h' : 'brook',
               '2a': 'bpawn', '2b' : 'bpawn', '2c': 'bpawn', '2d' : 'bpawn', '2e': 'bpawn', '2f' : 'bpawn', '2g': 'bpawn', '2h' : 'bpawn',
               '8a': 'wrook', '8b': 'wknight', '8c': 'wbishop', '8d': 'wqueen', '8e': 'wking', '8f' : 'wbishop', '8g' : 'bpawn', '8h' : 'wrook', # Extra pawn instead of second Knight!
               '7a': 'wpawn', '7b' : 'wpawn', '7c': 'wpawn', '7d' : 'wpawn', '7e': 'wpawn', '7f' : 'wpawn', '7g': 'wpawn', '7h' : 'wpawn',}

testBoard03 = {'1a': 'brook', '1b': 'bknight', '1c': 'bbishop', '1d': 'bqueen', '1e': 'bking', '1f' : 'bbishop', '1g' : 'bknight', '1h' : 'brook',
               '2a': 'bpawn', '2b' : 'bpawn', '2c': 'bpawn', '2d' : 'bpawn', '2e': 'bpawn', '2f' : 'bpawn', '2g': 'bpawn', '2h' : 'bpawn',
               '8a': 'wrook', '8b': 'wknight', '8c': 'wbishop', '8d': 'wqueen', '8f' : 'wbishop', '8g' : 'wknight', '8h' : 'wrook', # Missing a king!
               '7a': 'wpawn', '7b' : 'wpawn', '7c': 'wpawn', '7d' : 'wpawn', '7e': 'wpawn', '7f' : 'wpawn', '7g': 'wpawn', '7h' : 'wpawn',}

letters = 'abcdefgh'
numbers = '12345678'

BOARDUNITS = { 'pawn' : 0, 'rook' : 0, 'knight' : 0, 'bishop' : 0, 'queen' : 0, 'king' : 0 }
TOTALUNITS = { 'pawn' : 16, 'rook' : 4, 'knight' : 4, 'bishop' : 4, 'queen' : 2, 'king' : 2 }

def isValidChessBoard(board):

    boardIsValid = True

    for k in board:
        let = str(k[1:]).lower()
        num = str(k[:1]).lower()

        if let not in letters:
            print('INVALID: Letter is not in acceptable range!')
            boardIsValid = false
            return boardIsValid

        if num not in numbers:
            print('INVALID: Numbers are not in acceptable range!')
            boardIsValid = false
            return boardIsValid

        # print('Number is ' + num + ' and Letter is ' + let + '.')

    wPieces = bPieces = BOARDUNITS

    for k in board:
        # print('K is now: ' + str(k) + ' which is equal to ' + board[k])
        #piece = board[k]

        print(str(wPieces['king']) + ' white King(s), ' + str(bPieces['king']) + ' black King(s)')

        if board[k][0] == 'b':
            #print('First character of piece is ' + piece[0])

            if(board[k][1:] in bPieces):
                if(board[k][1:] == 'king'):
                    print('Black king here!')
                #print('[][] Black ' + piece[1:] + ' is in bPieces!')
                print('Before Increment: ' + str(wPieces['king']) + ' white King(s), ' + str(bPieces['king']) + ' black King(s)')
                bPieces[board[k][1:]] += 1
                print('After Increment: ' + str(wPieces['king']) + ' white King(s), ' + str(bPieces['king']) + ' black King(s)')
            #else:
            #   print('#### ' + piece[1] + ' is not found in bPieces.')

        elif board[k][0] == 'w':
            #print('First character of piece is ' + piece[0])

            if(board[k][1:] in wPieces):
                if(board[k][1:] == 'king'):
                    print('White king here!')
                print('[][] White '+ board[k][1:] + ' is in wPieces!')
                wPieces[board[k][1:]] += 1
            else:
                print('#### ' + board[k][1] + ' is not found in wPieces.')
        else:
            print('Playing piece is missing "black" or "white" characteristic.')
            return False


        for k in bPieces:
            #print('We have ' + str(bPieces[k]) + ' Black ' + str(k) + '(s)')
            #print('We have ' + str(wPieces[k]) + ' White ' + str(k) + '(s)')
            if bPieces[k] > TOTALUNITS[k] or wPieces[k] > TOTALUNITS[k]:

                print('INVALID: Too many pieces!')

                boardIsValid = False
    print(str(wPieces['king']) + ' white King(s), ' + str(bPieces['king']) + ' black King(s)')
    if bPieces['king'] <= 0 or wPieces['king'] <= 0:
        print('INVALID: One of our kings is missing.')
        #print('Black kings number: ' + str(bPieces['king']))
        #print('White kings number: ' + str(wPieces['king']))
        boardIsValid = False    

    return boardIsValid

if isValidChessBoard(testBoard03):
    print("Board is valid!")
else:
    print("Board is invalid.")
…我得到了黑国王的预期增量,但我也得到了白国王的增量。我很困惑。这里是否缺少某种全局变量,或者我只是在编写乱七八糟的代码


我真的很想在这双眼睛上多看一眼。提前感谢您,Rev.

错误在下面一行:

wPieces = bPieces = BOARDUNITS
此语句的目的是创建BOARDUNITS字典的两个铜片。相反,此行将
wPieces
bPieces
变量分配给对同一字典对象的引用。因此,修改
wpices
的条目也将修改
bpiecs
,并且也将修改
BOARDUNITS
,因为所有三个标识符指向同一个对象

您可以创建
BOARDUNITS
对象的副本,如下所示:

wPieces = BOARDUNITS.copy()
bPieces = BOARDUNITS.copy()

下面的问题有一个很好的解决方案,但请注意,在我的测试板中也会发现上面复制的错误(如果您想自己使用此代码!)
wPieces = BOARDUNITS.copy()
bPieces = BOARDUNITS.copy()