Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中根据用户输入以不同的值显示网格_Python - Fatal编程技术网

在Python中根据用户输入以不同的值显示网格

在Python中根据用户输入以不同的值显示网格,python,Python,我正在用Python完成一个战舰游戏项目。我一直坚持在网格上显示放置的船只。这是我的函数,它根据用户输入的坐标显示一个更新后的板,坐标的形式是字母,数字,如A10。用户网格是一个10x10的盒子。空板上印着“O”,垂直船上印着|字符,而水平船上印着--。这里是我的功能更新后,所有的坐标板已设置 def print_updated_board(coords, direction): board = [] for row in range(10): board_row

我正在用Python完成一个战舰游戏项目。我一直坚持在网格上显示放置的船只。这是我的函数,它根据用户输入的坐标显示一个更新后的板,坐标的形式是字母,数字,如A10。用户网格是一个10x10的盒子。空板上印着“O”,垂直船上印着|字符,而水平船上印着--。这里是我的功能更新后,所有的坐标板已设置

def print_updated_board(coords, direction):
    board = []
    for row in range(10):
        board_row = []
        updated = []
        for c in range(ord('a'), ord('a') + BOARD_SIZE):
            t = chr(c) + str(row)
            board_row.append(t)
        if direction == 'v':
            for coord in coords:
                for w in board_row:
                    if coord == w:
                        updated.append(VERTICAL_SHIP)
                    else:
                        updated.append(EMPTY)
        board.append(updated)
    print_board_heading()
    row_num = 1
    for row in board:
        print(str(row_num).rjust(2) + " " + (" ".join(row)))
        row_num += 1
坐标系是根据船只的大小创建的,所以四艘大小的战列舰示例垂直放置在a1处,将具有坐标(a1、a2、a3、a4)。我只是想在这个例子中,在这些坐标上打印a |,留下空坐标作为O

我的代码现在关闭了——网格似乎错误地打印了50行(而不是10行)

如有任何关于在何处进行此操作的指导,我们将不胜感激。谢谢

编辑****************意识到我是双循环,并将代码更改为此。还不能完美地工作(只差一个位置),但正在工作

def print_updated_board(coords, direction):
    board = []
    for row in range(10):
        board_row = []
        updated = []
        for c in range(ord('a'), ord('a') + BOARD_SIZE):
            t = chr(c) + str(row)
            board_row.append(t)
        if direction == 'v':
            for b in board_row:
                if b in coords:
                    updated.append(VERTICAL_SHIP)
                else:
                    updated.append(EMPTY)
            board.append(updated)
    print_board_heading()
    row_num = 1
    for row in board:
        print(str(row_num).rjust(2) + " " + (" ".join(row)))
        row_num += 1

问题在于这些嵌套循环:

for coord in coords:
    for w in board_row:
        updated.append(...)
这会导致列表中每个坐标的板变宽


最好的解决方案是将所有这些代码扔出窗口,因为有一种更简单的方法:

def print_updated_board(coords, direction):
    # create an empty board
    board = [['O']*BOARD_SIZE for _ in range(BOARD_SIZE)]
    # at each coordinate, draw a ship
    for coord in coords:
        # convert string like "a1" to x,y coordinates
        y= ord(coord[0])-ord('a')
        x= int(coord[1:])-1
        # update the board at this position
        board[x][y]= '|' if direction=='v' else '--'

好吧,我确定了我的问题——我是双循环的——一次在板上,另一次在每艘船的坐标上。修改了上面的代码以反映——我的一个问题是船正在打印一个偏离标记的变量。快速问题——让我困惑的一个部分是转换字符串的位置……只是不理解减法部分。正如我所想的,它可能与Python的索引属性有关——从0开始,等等。是吗?@JohnRogerson确实是这样,减法使我们的坐标从0开始。太好了,谢谢。在放置每艘船后,有没有关于如何更新船上所有船只的提示?现在,它正在向董事会展示每一艘船只guess@JohnRogerson那就把第一行去掉。与其创建一个空板,不如将该板作为参数传递,并让它更新现有的板。嗯,尝试过这样做,但列表索引一直超出范围--与板[x][y]对齐=