pythonpython-3.xplaying-cards

check overlap Elements in Playing Cards List


i just ask here for the first time.

i made the BlackJack for discord.py but sometimes this was returned me 'None' so i tried the debug and found the cause.

def deck(self):
    card_type = ["Club ", "Diamond ", "Spade ", "Heart "]
    card_nums = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "K", "Q", "A"]
    cards_total = [i + j for i in card_type for j in card_nums]
    random.shuffle(cards_total)
    pop_card = cards_total.pop()

    if pop_card in self.overlap_check:
        Test.deck(self)
        return
    self.overlap_check.append(pop_card)
    return self.overlap_check[-1]

if i tried this itself, it works fine. but if i call the func = deck(self), sometimes i could see the return result to None. according i think this error is occured by there.

    if pop_card in self.overlap_check:
        Test.deck(self)
        return

but i couldn't think more how to remake this Algorithm. please let me know how to overap check in list and break.


Solution

  • Update your If block by -

        if pop_card in self.overlap_check:
            return Test.deck(self)
    

    The problem is : when pop_card in self.overlap_check is true then script is calling the function 2nd time here: Test.deck(self) so lets say the 2nd call of the function returned "Club 2". Now next line return will be executed and it will return None. Hence remove that line directly return the value which the 2nd function call is returning.