Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionary Referencing
#1
Hi all,

I'm writing a pretty rudimentary code that essentially plays chess with itself. Dictionaries are the data types used to contain all of the data about each individual piece. I have only programmed the movement of pawns this far. I've been using functions for most of the content but I'm having an issue with one of the functions referencing a dictionary. I'm trying to reference an input from the function using an if statement, but the function simply skips over it and executes the else statement. Attached below is the chess code and subsequent UDF code. The code below is the function causing issues.

P.S. I am interested in hearing any good advice on optimizing the code as it is.

Thanks.

def pawn(board,color,which):
    if color == white:
        nr = -1
        spot = 'W_pawn'
    else:
        nr = 1
        spot = 'B_pawn'
        
    # empty current pos
    board[color['Pawns']['Pawn %s' %(which)]['Row'],color['Pawns']['Pawn %s' %(which)]['Column']] = ''
    
    # edit row value in dictionary
    color['Pawns']['Pawn %s' %(which)]['Row'] += nr
    
    # update new pos in board
    board[color['Pawns']['Pawn %s' %(which)]['Row'],color['Pawns']['Pawn %s' %(which)]['Column']] = spot

    return board,color

Attached Files

.txt   ChessBoard.txt (Size: 566 bytes / Downloads: 197)
.py   ChessUDF.py (Size: 7.75 KB / Downloads: 190)
.py   Robots Play Chess 1.2.py (Size: 2.07 KB / Downloads: 183)
Reply


Messages In This Thread
Dictionary Referencing - by nickdavis2017 - Nov-19-2021, 08:27 PM
RE: Dictionary Referencing - by deanhystad - Nov-20-2021, 06:24 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  name 'lblstatus' is not defined when referencing a label KatManDEW 4 1,665 Apr-21-2022, 12:33 PM
Last Post: KatManDEW
  Referencing string names in df to outside variables illmattic 1 1,428 Nov-16-2021, 12:47 PM
Last Post: jefsummers
  Referencing a fixed cell Mark17 2 2,160 Dec-17-2020, 07:14 PM
Last Post: Mark17
Sad need help in referencing a list n00bdev 2 1,908 Nov-01-2020, 12:06 PM
Last Post: buran
  Issue referencing new instance from other class nanok66 3 2,313 Jul-31-2020, 02:07 AM
Last Post: nanok66
  referencing another method in a class Skaperen 6 2,784 Jul-02-2020, 04:30 AM
Last Post: Skaperen
  Theory behind referencing a dictionary rather than copying it to a list sShadowSerpent 2 2,168 Mar-24-2020, 07:18 PM
Last Post: sShadowSerpent
  "not defined" error in function referencing a class Exsul 2 3,764 Mar-27-2019, 11:59 PM
Last Post: Exsul
  Prevent Variable Referencing Th3Eye 4 2,983 Oct-03-2018, 07:01 PM
Last Post: buran
  Tree insertion and variable referencing hshivaraj 3 3,417 Dec-10-2017, 04:29 PM
Last Post: Windspar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020