局部变量“i”可能在赋值之前被引用

2024-04-26 12:31:32 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在Python3.9.1中消除这个bug

我需要返回“I”还是可以返回为空

def pick_up_card(self, player, amount=1):
   
    for i in range(1, amount+1):
        # if no more card in stock pile
        if not self.stock:
            # add back discarded cards (but not top card)
            if len(self.discards) == 1:
                UI.print_message("All cards distributed")
                return i-1
            self.stock = self.discards[:-1]
            del self.discards[:-1]
            # shuffle stock
            random.shuffle(self.stock)
            UI.print_message("Discards are shuffled back.")
        # draw stock card
        card = self.stock.pop()
        # and add to hand
        player.hand.append(card)
    return i

Tags: inselfadduimessageifstockback