0

How can I iron out this bug in python 3.9.1.

Do I need to return 'i' or can return be blank?

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
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
rowan333
  • 15
  • 5
  • 6
    If the loop is never entered, what value do you want `i` to have? – Carcigenicate Jan 03 '21 at 13:22
  • if you want to return `i` you have to define it before the for loop, but you can also return nothing/empty (which means it returns `None`). It's up to you, what you want to do with the return value – D-E-N Jan 03 '21 at 13:29
  • 2
    @D-E-N Technically you don't *have* to if you can guarentee that the iterator being iterated will be non-empty. Without such a guarentee though, ya, assigning it before the loop is the simplest fix. – Carcigenicate Jan 03 '21 at 13:35
  • 1
    yeah, that's true, but i would recommend to not "trust" this guarentee, you should use a default value (and "good" IDEs will point this out ;)) – D-E-N Jan 03 '21 at 13:42
  • @D-E-N well, if you *expect* the loop to always be entered then probably *you want the error* – juanpa.arrivillaga Jan 03 '21 at 14:25

0 Answers0