python中带函数的简单映射

python中带函数的简单映射,python,Python,只是尝试使用该函数制作简单的地图,并将玩家位置包含在地图中,但玩家不会出现(地图为空) 这是我的密码。请帮忙解决。谢谢 import random CELLS = [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (0, 1), (1, 1), (2, 1), (3, 1), (4, 1), (0, 2), (1, 2), (2, 2), (3, 2), (4, 2), (0, 3), (1, 3), (2,

只是尝试使用该函数制作简单的地图,并将玩家位置包含在地图中,但玩家不会出现(地图为空)

这是我的密码。请帮忙解决。谢谢

import random

CELLS = [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0),
         (0, 1), (1, 1), (2, 1), (3, 1), (4, 1),
         (0, 2), (1, 2), (2, 2), (3, 2), (4, 2),
         (0, 3), (1, 3), (2, 3), (3, 3), (4, 3),
         (0, 4), (1, 4), (2, 4), (3, 4), (4, 4)]

def get_locations():

    return random.sample(CELLS, 1)

player = get_locations()

def draw_map(player):

    print(" _" * 5)
    tile = "|{}"


    for cell in CELLS:
        x, y = cell

        if x < 4:
            line_end = ""
            if cell == player:
                output = tile.format("X")
            else:
                output = tile.format("_")
        else:
            line_end = "\n"
            if cell == player:
                output = tile.format("X|")
            else:
                output = tile.format("_|")
        print(output, end=line_end)


draw_map(player)
随机导入
单元格=[(0,0)、(1,0)、(2,0)、(3,0)、(4,0),
(0, 1), (1, 1), (2, 1), (3, 1), (4, 1),
(0, 2), (1, 2), (2, 2), (3, 2), (4, 2),
(0, 3), (1, 3), (2, 3), (3, 3), (4, 3),
(0, 4), (1, 4), (2, 4), (3, 4), (4, 4)]
def get_位置():
返回随机样本(单元格,1)
player=获取位置()
def绘制地图(玩家):
打印(“u”*5)
tile=“|{}”
对于单元中的单元:
x、 y=单元
如果x<4:
行_end=“”
如果单元格==播放器:
输出=tile.format(“X”)
其他:
输出=tile.format(“\”)
其他:
行\u end=“\n”
如果单元格==播放器:
输出=tile.format(“X |”))
其他:
输出=tile.format(“124;”)
打印(输出,结束=行\结束)
绘制地图(玩家)

您需要
随机。选择(单元格)
,而不是
随机。样本(单元格,1)

返回一个随机元素out
seq

(1, 3)
返回包含一个随机元素的
seq
子列表:

[(1, 3)]
通过此小更改,您的程序输出:

 _ _ _ _ _
|_|_|_|_|_|
|_|_|_|_|_|
|_|_|_|_|_|
|_|X|_|_|_|
|_|_|_|_|_|

您需要
random.choice(单元格)
,而不是
random.sample(单元格,1)

返回一个随机元素out
seq

(1, 3)
返回包含一个随机元素的
seq
子列表:

[(1, 3)]
通过此小更改,您的程序输出:

 _ _ _ _ _
|_|_|_|_|_|
|_|_|_|_|_|
|_|_|_|_|_|
|_|X|_|_|_|
|_|_|_|_|_|