0

这是伪代码,只是为了简单地解释我想要做的事情。

x = [1, 2, 3, 4, 5, 6]

def func_1(x):
    a = ...
    b = ...
    if a == 2 and b == 5:
        do this to the list...


def func_2(x):
    if a == 6 and b == 2:
        do this to the list...

这是伪代码,只是为了简单地解释我想要做的事情。

我希望能够...

a) 在一个函数中运行两个单独的 if,但要分别处理它们

b) 具有单独的函数,但将 a 和 b 的值从第一个函数传递到另一个函数(或第一个函数末尾的调整列表作为第二个函数的参数)

当我打电话...

x = func_1(x)
x = func_2(x)

它说...

<function func_1 at 0x244384>

当我把它全部放在一个函数中时,它也会做同样的事情:(

下面是工作(或在这种情况下不工作)代码。警告它很长。在 Deploy_Destroyer_2 中实现我想要的可能有更简单的方法,但除非这是问题所在,否则我不会对此提出建议。

def InitBoards():
    Player = [[1]*(6 if x<6 else 12) for x in range(12)]
    Opponent = [[0]*(6 if x<6 else 12) for x in range(12)]

    return Player,Opponent

def Deploy_Destroyer_1(Player):
    rand_col_1 = randint(0,11)
    if rand_col_1 < 6:
        rand_row_1 = randint(0,11)
        return rand_col_1
        return rand_col_2
    else:
        rand_row_1 = randint(6,11)
        return rand_col_1
        return rand_row_1
    if Player[rand_row_1][rand_col_1] == 2:
        Deploy_Destroyer_1(Player)


def Deploy_Destroyer_2(Player):
    if rand_col_1 == 5 and rand_row_1 == 6:
        #can be 1, 2, 3 or 4... in that order below
        rand_position_1 = randint(1,4)
        if rand_position_1 == 1:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 + 1][rand_col_1] = 2
        if rand_position_1 == 2:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 - 1][rand_col_1] = 2
        if rand_position_1 == 3:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 + 1] = 2
        if rand_position_1 == 4:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 - 1] = 2
    elif rand_col_1 in range(1,4) and rand_row_1 in range(1,10):
        #can be any 1, 2, 3 or 4... in that order below
        rand_position_1 = randint(1,4)
        if rand_position_1 == 1:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 + 1][rand_col_1] = 2
        if rand_position_1 == 2:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 - 1][rand_col_1] = 2
        if rand_position_1 == 3:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 + 1] = 2
        if rand_position_1 == 4:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 - 1] = 2
    elif rand_col_1 in range(5,10) and rand_row_1 in range(7,10):
        #can be any 1, 2, 3 or 4... in that order below
        rand_position_1 = randint(1,4)
        if rand_position_1 == 1:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 + 1][rand_col_1] = 2
        if rand_position_1 == 2:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 - 1][rand_col_1] = 2
        if rand_position_1 == 3:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 + 1] = 2
        if rand_position_1 == 4:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 - 1] = 2
    elif rand_col_1 == 0 and rand_row_1 == 0:
        #can be any 1, 2, 3 or 4... in that order below
        rand_position_1 = randint(1,4)
        if rand_position_1 == 1:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 + 1][rand_col_1] = 2
        if rand_position_1 == 2:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 - 1][rand_col_1] = 2
        if rand_position_1 == 3:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 + 1] = 2
        if rand_position_1 == 4:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 - 1] = 2
    elif (rand_col_1 == 5 and rand_row_1 == 0) or (rand_col_1 == 11 and rand_row_1 ==6):
        #can be one or four
        #check brackets and booleans here
        rand_position_1 = randint(1,2)
        if rand_position_1 == 1: #position 1
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 + 1][rand_col_1] = 2
        if rand_position_1 == 2: #position 4
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 - 1] = 2
    elif rand_col_1 == 0 and rand_row_1 == 11:
        #can be 2 or 3
        rand_position_1 = randint(2,3)
        if rand_position_1 == 2: #position 2
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 - 1][rand_col_1] = 2
        if rand_position_1 == 3: #position 3
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 + 1] = 2
    elif rand_col_1 == 11 and rand_row_1 == 11:
        #can be 2 or 4
        rand_position_1 = randint(1,2)
        if rand_position_1 == 1: #position 2
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 - 1][rand_col_1] = 2
        if rand_position_1 == 2: #position 4
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 - 1] = 2
    elif (rand_row_1 == 0 and rand_col_1 in range(1,4)) or (rand_row_1 == 6 and rand_col_1 in range(6,10)):
        #can be 1, 3 or 4
        #check brackets and booleans here
        rand_position_1 = randint(1,3)
        if rand_position_1 == 1: #position 1
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 + 1][rand_col_1] = 2
        if rand_position_1 == 2: #position 3
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 + 1] = 2
        if rand_position_1 == 3: #position 4
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 - 1] = 2
    elif (rand_col_1 == 5 and rand_row_1 in range(1,5)) or (rand_col_1 == 11 and rand_row_1 in range(7,10)):
        #can be 1, 2 or 4
        #check brackets and booleans here
        rand_position_1 = randint(1,3)
        if rand_position_1 == 1: #position 1
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 + 1][rand_col_1] = 2
        if rand_position_1 == 2: #position 2
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 - 1][rand_col_1] = 2
        if rand_position_1 == 3: #position 4
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 - 1] = 2
    elif rand_col_1 == 0 and rand_row_1 in range(1,10):
        #can be 1, 2 or 3... in that order below
        rand_position_1 = randint(1,3)
        if rand_position_1 == 1:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 + 1][rand_col_1] = 2
        if rand_position_1 == 2:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 - 1][rand_col_1] = 2
        if rand_position_1 == 3:
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 + 1] = 2
    elif rand_col_1 in range(1,10) and rand_row_1 == 11:
        #can be 2, 3 or 4
        rand_position_1 = randint(1,3)
        if rand_position_1 == 2: #position 2
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1 - 1][rand_col_1] = 2
        if rand_position_1 == 3: #position 3
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 + 1] = 2
        if rand_position_1 == 4: #position 4
            Player[rand_row_1][rand_col_1] = 2
            Player[rand_row_1][rand_col_1 - 1] = 2
    return Player


Player, Opponent = InitBoards()
Player = Deploy_Destroyer_1
Player = Deploy_Destroyer_2

print Player
print Opponent

有人有什么想法吗?或者上面的错误是什么意思?

4

2 回答 2

1

列表是可变对象,所以答案是“就这么做”。它会像魔术一样工作(虽然在大多数情况下,这是一个陷阱)。

而且我应该更多地查看细节,以使a 和 b进入第二个函数的范围,许多可能的解决方案之一是:

def f1(l):
    …
    return l, a, b

def f2(l, a, b):
    …

f2(*f1(x))

如果我们只讨论 f1、f2、a 和 b,而不是问题域,则无法提出合适的解决方案。;)

于 2013-09-21T18:25:28.910 回答
0

What is wrong with this:

x = [1, 2, 3, 4, 5, 6]

def f(x):
    a = ...
    b = ...
    if a == 2 and b == 5:
        # do this to the list...
        pass

    if a == 6 and b == 2:
        # do this to the list...
        pass

    return x

x = f(x)

?

(on a side note, the above is legal Python 3.x, as ... is an inbuilt object!)

于 2013-09-21T18:35:27.510 回答