首页 > 解决方案 > 用于嵌套列表中的列表的 if-in 语句测试无法识别第一个列表在函数中使用时位于第二个列表中

问题描述

if-in 语句应该打印 'test',但它不会。每当我在函数之外尝试类似的想法时,它都能完美运行,就像底部的示例一样。我可以说问题出在 if in 语句上,但我不明白为什么它不起作用。我能做些什么来解决这个问题吗?

class Pawn:

    def __init__(self, num, pos, col, fmove):
        self.num = num
        self.pos = pos
        self.col = col
        self.fmove = fmove

w_pawn1 = Pawn(1, [1, 2], 'White', True)

def move(piece, typ):

    if typ == 'pawn':
        
        if piece.fmove == True:


            posmove = [[piece.pos[0], int(piece.pos[1])+1],
                       [piece.pos[0], int(piece.pos[1])+2]]

            Move = input('Where do you want to move?')

            print(Move)
            print(posmove)

            if Move in posmove:
                print('test')
                piece.pos = move

    else: pass



list1 = [1,3]
list2 = [[1,3],[1,4]]

if list1 in list2:
    print('ok')

output:ok

标签: pythonfunctionif-statement

解决方案


推荐阅读