使用 for 循环读取值时,出现此错误(ValueError:以 10 为基数的 int() 的文字无效:'')

问题描述 投票:0回答:1

我正在尝试制作一个可以在终端上运行的国际象棋游戏,并且应该有值的变量没有值,我不知道问题是什么

我想制作一个国际象棋引擎,但在制作国际象棋引擎之前,我试图制作一个可以在终端上看到的国际象棋游戏,如果您愿意,您可以玩国际象棋游戏并可以从终端操纵棋盘。我正在尝试使用一个名为 colomn_m 的变量来使用 for 循环读取该棋子列的国际象棋移动的最后一位数字,并且我例外地让它具有该值,但它没有,即使我尝试输入一个有效的值像pf5一样移动

这是代码 |........................................................ ………… ........................|/


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} "

请帮助我

python null valueerror python-chess
1个回答
0
投票

continue
块中的
case
语句会跳过
m_c = 2
,因此再次执行
if m_c == 1:
块,而不是第三个输入字符的
if m_c == 2:
块,因此不会分配给
colomn_m
.

我会将这个不必要的复杂

for
循环替换为

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

(请注意,

row_m
colomn_m
是混淆的)。

© www.soinside.com 2019 - 2024. All rights reserved.