【问题标题】:Navigate path through the 2d list by input通过输入在 2d 列表中导航路径
【发布时间】:2020-10-12 19:33:54
【问题描述】:

如何使用以下输入在二维列表中导航路径:u、d、l、r 分别表示上、下、左、右。参考将是 list[0][0]=1(参见 ex. fig. ),它使用 python reverse 方法使 [0][0] 出现在底部。如果移动超出范围,它将忽略该移动并继续下一步移动。 (例如:输入 uuddlrlr 将向上两次,向下两次,向左,向右,然后向左,再次向右)。最终会出现在 list[0][1] = 8 中。

图1使用python reverse方法使[0][0]出现在底部。

list = [[0,0,0,0],
        [0,0,0,0],
        [0,0,0,0],
        [0,8,0,0]]


I have try this code though but ain't working
def __init__(self, move):
    self.move = move
    self.x = ''

def inputMove(self, Monster):
    self.content = Monster.getVal()

    for self.row in reversed(self.content):
        print(self.row)
    print(isinstance(self.move,str))

    try:
        self.i = 0
        self.j = 0
        self._doraPower = 10
        isinstance(self.move, str)

        if self.move == 'u':
            try:
                self.i+=1
                self.content[self.i][self.j]
                for self.row in reversed(self.content):
                    print(self.row)
                if self._doraPower >= self.content[self.i][self.j]:
                    print('dora wins')
                else:
                    print('Coordinates of last room visited:',self.i,',',self.j)
            except IndexError:
                pass

        if self.move == 'd':
            try:
                self.i-=1
                self.content[self.i][self.j]
                print(self.i)
                for self.row in reversed(self.content):
                    print(self.row)
                if self.i > 0:    
                    if self._doraPower >= self.content[self.i][self.j]:
                        print('dora wins')
                    else:
                        print('Coordinates of last room visited:',self.i,',',self.j)
                else:
                    pass
            except IndexError:
                pass

        if self.move == 'r':
            try:
                self.j+=1
                self.content[self.i][self.j]

                for self.row in reversed(self.content):
                    print(self.row)
                
                if self._doraPower >= self.content[self.i][self.j]:
                    print('dora wins')
                else:
                    print('Coordinates of last room visited:',self.i,',',self.j)
            except IndexError:
                pass
            
        if self.move == 'l':
            try:
                self.j-=1
                self.content[self.i][self.j]
                print(self.i, self.j)
                if self.j > 0:
                    if self._doraPower >= self.content[self.i][self.j]:
                        print('dora wins')
                        print(self.content[self.i][self.j])
                    else:
                        print('Coordinates of last room visited:',self.i,',',self.j)
                else:
                    pass

            except IndexError:
                pass

        else:
            pass

    except ValueError:
        pass

【问题讨论】:

    标签: python python-3.x list oop


    【解决方案1】:

    这段代码看起来像是属于一个类。此外, self.content[self.i][self.j] 似乎没有做任何事情。至于索引错误,我会仔细检查输入数据以确保不是问题所在。

    Monster.getVal() 是否返回类似列表的矩阵?

    【讨论】:

    • 如果索引错误它会通过,因为代码捕获了错误
    猜你喜欢
    • 2018-11-08
    • 1970-01-01
    • 2021-08-13
    • 2019-05-24
    • 1970-01-01
    • 2022-11-29
    • 2021-12-30
    • 2022-10-24
    • 1970-01-01
    相关资源
    最近更新 更多