我的代码如下(我不知道为什么它没有缩进):
def move_joker_2(deck):
#start checking every card in the deck until the position of
#JOKER2 is found
#we start from position 0
position = 0
while deck [position] != JOKER2:
position = position + 1
#the new position for JOKER2
if position < len (deck) - 3:
new_position = position + 2
elif position == len (deck) - 2:
new_position = 0
else:
new_position = 1
#to reorganize deck, we need 2 things
# 1.) a list of the cards above JOKER2 after moving the cards
# 2.) a list of the cards below JOKER2 after moving the cards
#depending of new_position, there are 3 possible new decks
if new_position == 0:
#no cards above, JOKER2 will become the top card
cards_above = []
#every other card will be below it
cards_below = deck
#remove JOKER2, since we moved it
cards_below.pop(position)
elif new_position == 1:
#the only card above JOKER2 will be the top card
cards_above = [deck[0]]
#every other card up except the last one will be below it
cards_below = deck [new_position:len(deck)-1]
else:
cards_above = deck[0:new_position+1]
#remove JOKER2, since we moved it
cards_above.pop(position)
cards_below = deck [new_position+1:]
#final deck
deck = cards_above + [JOKER2] + cards_below
我的代码收到一个字符串列表并最终对其进行变异...
但是为什么它不改变原来的列表呢?例如:
甲板 = [1, 3, 27, 8, 9] move_joker_2(甲板)
考虑到 JOKER2 是 27,它应该将列表更改为: [1, 3, 8, 9, 27]
但是每当我打电话给deck时,它都没有改变......