Range / empty list issues??

Joined
Jul 20, 2023
Messages
56
Reaction score
2
I cant believe how much of a pain I'm having with this right of the bat. I mean I'm loving it just as much but just surprised. I've already conquered dozens of road blocks but I'm getting ready to go to bed. I wonder if someone will have an answer for this by the time I'm up. Same blackjack projects, I've made the amount of cards in a deck much smaller to make cycling through it much faster in testing. When a suit is randomly choses that doesnt have anymore cards left (aka an empty list), I call the generate_random_card function again to just chose another one. Its only 4 choices, shouldnt be a big deal. But the if statement doesnt seam to be catching it. First I had it as this " if available_cards[suit] == []: generate_random_card() " Then I changed it to " if not available_cards[suit]: generate_random_card() ". But neither worked.
Python:
from random import randint, randrange
from copy import deepcopy

# PLAYER AND DEALERS WALLET CASH VALUES
players_wallet = 500
dealers_wallet = 500

# SET CONSTANT VALUES OF CARD SUITS AND FACES TO CREATE DECK
# card_numbers = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
card_numbers = ['A', '2', '3', '4', 'Q', 'K']
suits = ["hearts", "clubs", "spades", "diamonds"]

# ADDITIONAL VARIABLES AND COPIES OF VARS TO MANIPULATE DECK AFTER PULLING CARDS
cards_left_after_removal = []
picked_cards = []

# CREATE FULL 52 CARD DECK
full_deck = {}
for i in range(len(suits)):
    full_deck[suits[i]] = [x for x in card_numbers]
available_cards = deepcopy(full_deck)
# available_cards = full_deck.copy()  # COPY DECK FOR CARD REMOVAL

def generate_random_card():
    # RANDOM NUMBER (0-3) - (HEARTS, CLUBS, SPADES, DIAMONDS)
    random_suit = randrange(0, len(available_cards))
    # IF THE SUIT IS EMPTY GENERATE NEW RANDOM NUMBER
    if not available_cards[suits[random_suit]]:
        print(f'------------ {suits[random_suit].upper()} EMPTY -------------')
        generate_random_card()
    # RANDOM CARD NUMBER OUT OF HOW MANY ARE LEFT FOR THE SUIT
    random_card_index = randrange(0, len(available_cards[suits[random_suit]]))
    # A TEXT REPRESENTATION OF THE EXACT CARD PICKED
    random_card = f'{available_cards[suits[random_suit]][random_card_index]} of {suits[random_suit]}'
    # APPEND TO COLLECTION OF PICKED CARDS
    picked_cards.append(random_card)
    # PRINT CARD PICKED TO SCREEN
    print('1.HEARTS 2.CLUBS 3.SPADES 4.DIAMONDS')
    print(f'----RAW NUMBER DATA--- \nRandom Suit #:{random_suit} Random card index #:{random_card_index}')
    print(f'Length of {suits[random_suit]}: {len(available_cards[suits[random_suit]])}  :: {available_cards[suits[random_suit]]}')
    print(f'Random pick: {random_card}')
    # CALLING remove_card_from_deck()
    remove_card_from_deck(random_suit, random_card_index)
    print('Available Cards:')
    print('\n'.join([f'{k}: {v}' for k, v in available_cards.items()]))
    print()


def remove_card_from_deck(suit, num):
    available_cards[suits[suit]].pop(num)

while True:
    generate_random_card()
    print(f'Your cards so far:\n{picked_cards}')
    low_num = 0
    for key in available_cards:
        low_num += len(available_cards[key])
    if low_num <= 0:
        print('Game over')
        break

    ans = input("Pick another card? (Y/N):")
    if ans.lower() == 'n':
        break
 
Joined
Jan 24, 2024
Messages
42
Reaction score
7
The issue you're facing is that when generate_random_card() is recursively called in the case of an empty suit, the new random suit and card are generated, but the result is not propagated back to the calling function. This is because the recursive call is not returning any value.

To fix this, you need to modify the line where you call generate_random_card() inside the function. Instead of just calling it, you need to return its result, and then check if the current suit is empty. Here's the modified part:

def generate_random_card():
# RANDOM NUMBER (0-3) - (HEARTS, CLUBS, SPADES, DIAMONDS)
random_suit = randrange(0, len(available_cards))
# IF THE SUIT IS EMPTY GENERATE NEW RANDOM NUMBER
if not available_cards[suits[random_suit]]:
print(f'------------ {suits[random_suit].upper()} EMPTY -------------')
return generate_random_card() # Return the result of the recursive call
# RANDOM CARD NUMBER OUT OF HOW MANY ARE LEFT FOR THE SUIT
random_card_index = randrange(0, len(available_cards[suits[random_suit]]))
# A TEXT REPRESENTATION OF THE EXACT CARD PICKED
random_card = f'{available_cards[suits[random_suit]][random_card_index]} of {suits[random_suit]}'
# APPEND TO COLLECTION OF PICKED CARDS
picked_cards.append(random_card)
# PRINT CARD PICKED TO SCREEN
print('1.HEARTS 2.CLUBS 3.SPADES 4.DIAMONDS')
print(f'----RAW NUMBER DATA--- \nRandom Suit #:{random_suit} Random card index #:{random_card_index}')
print(f'Length of {suits[random_suit]}: {len(available_cards[suits[random_suit]])} :: {available_cards[suits[random_suit]]}')
print(f'Random pick: {random_card}')
# CALLING remove_card_from_deck()
remove_card_from_deck(random_suit, random_card_index)
print('Available Cards:')
print('\n'.join([f'{k}: {v}' for k, v in available_cards.items()]))
print()
return random_suit # Return the current suit index

# ...

while True:
last_suit = generate_random_card()
print(f'Your cards so far:\n{picked_cards}')
low_num = 0
for key in available_cards:
low_num += len(available_cards[key])
if low_num <= 0:
print('Game over')
break

ans = input("Pick another card? (Y/N):")
if ans.lower() == 'n':
break
else:
# Remove the last suit from available_cards if the user picks another card
remove_card_from_deck(last_suit, 0)

This modification ensures that the result of the recursive call is returned and used to remove the correct suit when the user decides to pick another card.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,836
Messages
2,569,747
Members
45,545
Latest member
rapter____0

Latest Threads

Top