pythonnullvalueerrorpython-chess

while reading a value by using a for loop it gives me this error (ValueError: invalid literal for int() with base 10: '')


I am trying to make a chess game that can be ran on terminal and a variable that should have a value doesn't have a value and i don't know what is that cousing the problem

I want to make a chess engine but before doing a chess engine i was trying to make a chess game kind of thing can be seen on the terminal and if you wish to you could play a chess game and can manipulate the board from the terminal. I was trying for a variable called colomn_m to read the last digit of the chess move for the column of the piece using a for loop and i was excepting to it to have the value but it doesn't, even if i tried entering a valid move like pf5

this the code |...................................................... ..............|/


board = [[" R ", " N ", " B ", " Q ", " K ", " B ", " N ", " R "],
         [" P ", " P ", " P ", " P ", " P ", " P ", " P ", " P "],
         ["   ", "   ", "   ", "   ", "   ", "   ", "   ", "   "],
         ["   ", "   ", "   ", "   ", "   ", "   ", "   ", "   "],
         ["   ", "   ", "   ", "   ", "   ", "   ", "   ", "   "],
         ["   ", "   ", "   ", "   ", "   ", "   ", "   ", "   "],
         [" p ", " p ", " p ", " p ", " p ", " p ", " p ", " p "],
         [" r ", " n ", " b ", " q ", " k ", " b ", " n ", " r "],]


m_c = 0

piece_m = ""
row_m = ""
colomn_m = ""

def draw_b():
    print(" +---+---+---+---+---+---+---+---+")
    print("8|{}|{}|{}|{}|{}|{}|{}|{}|".format(board[7][0], board[7][1], board[7][2], board[7][3], board[7][4], board[7][5], board[7][6], board[7][7]))
    print(" +---+---+---+---+---+---+---+---+")#emptyrom*********************************************************************************************
    print("7|{}|{}|{}|{}|{}|{}|{}|{}|".format(board[6][0], board[6][1], board[6][2], board[6][3], board[6][4], board[6][5], board[6][6], board[6][7]))
    print(" +---+---+---+---+---+---+---+---+")#emptyrom*********************************************************************************************
    print("6|{}|{}|{}|{}|{}|{}|{}|{}|".format(board[5][0], board[5][1], board[5][2], board[5][3], board[5][4], board[5][5], board[5][6], board[5][7]))
    print(" +---+---+---+---+---+---+---+---+")#emptyrom*********************************************************************************************
    print("5|{}|{}|{}|{}|{}|{}|{}|{}|".format(board[4][0], board[4][1], board[4][2], board[4][3], board[4][4], board[4][5], board[4][6], board[4][7]))
    print(" +---+---+---+---+---+---+---+---+")#emptyrom*********************************************************************************************
    print("4|{}|{}|{}|{}|{}|{}|{}|{}|".format(board[3][0], board[3][1], board[3][2], board[3][3], board[3][4], board[3][5], board[3][6], board[3][7]))
    print(" +---+---+---+---+---+---+---+---+")#emptyrom*********************************************************************************************
    print("3|{}|{}|{}|{}|{}|{}|{}|{}|".format(board[2][0], board[2][1], board[2][2], board[2][3], board[2][4], board[2][5], board[2][6], board[2][7]))
    print(" +---+---+---+---+---+---+---+---+")#emptyrom*********************************************************************************************
    print("2|{}|{}|{}|{}|{}|{}|{}|{}|".format(board[1][0], board[1][1], board[1][2], board[1][3], board[1][4], board[1][5], board[1][6], board[1][7]))
    print(" +---+---+---+---+---+---+---+---+")#emptyrom*********************************************************************************************
    print("1|{}|{}|{}|{}|{}|{}|{}|{}|".format(board[0][0], board[0][1], board[0][2], board[0][3], board[0][4], board[0][5], board[0][6], board[0][7]))
    print(" +---+---+---+---+---+---+---+---+")#emptyrom*********************************************************************************************
    print("   a   b   c   d   e   f   g   h  ")#emptyrom*********************************************************************************************

while True:
    draw_b()

    try:
        move = input("Enter your move: ")
    except ValueError:
        move = input("Please enter a correct move 'xxx piece_letter+the_colom+the_row': ")

    m_c = 0
    
    for i in move:
        if m_c == 0:
            piece_m = i
            m_c =1
            continue
            
        if m_c == 1:
            match i:
                case "a":
                    row_m = "1"
                    print("1")
                    continue
                case "b":
                    row_m = "2"
                    print("2")
                    continue
                case "c":
                    row_m = "3"
                    print("3")
                    continue
                case "d":
                    row_m = "4"
                    print("4")
                    continue
                case "e":
                    row_m = "5"
                    print("5")
                    continue
                case "f":
                    row_m = "6"
                    print("6")
                    continue
                case "g":
                    row_m = "7"
                    print("7")
                    continue
                case "h":
                    row_m = "8"
                    print("8")
                    continue
            m_c = 2
            continue

        if m_c == 2:
            colomn_m = str(i)
            print(colomn_m)
            m_c = 3
            continue

    m_c = 0

    print(piece_m)
    print(row_m)
    print(colomn_m)

    board[int(row_m)-1][int(colomn_m)-1] = f" {piece_m} "

please help me


Solution

  • The continue statements in the case blocks skip the m_c = 2, hence the if m_c == 1: block is again executed instead of the if m_c == 2: block for the third input character, which so isn't assigned to colomn_m.

    I'd substitute this unnecessarily complex for loop with

        piece_m  = move[0]
        colomn_m = 1+"abcdefgh".index(move[1])
        row_m    = move[2]
    

    (note that row_m and colomn_m were confounded).