Reputation: 1625
Hello I am doing a beginner project on my own by making a blackjack game. It is going well and I can even choice 1 or 11 for Ace values. My problem is that I am trying to remove the cards from the deck for myself and also when I add a Dealer.
I thought I could do the .remove() function to remove it but that doesnt seem to work after running through the program with http://www.pythontutor.com/
Here is the deck code I am using
from random import randint
def CardDeck():
#sets the card types and values
CardValue = ['Ace','2','3','4','5','6','7','8','9','10','J','Q','K']
CardType = ['Hearts','Spades','Clubs','Diamonds']
Deck = []
Card = randint(0,(len(CardValue )*len(CardType)))
#This iterates all 52 cards into a deck
for i in CardType:
for j in CardValue:
Deck.append(j + ' of ' + i)
temp = Deck[Card]
Deck.remove(Deck[Card]) #this should remove the card from the deck
return temp
Upvotes: 0
Views: 167
Reputation: 1625
Thank you all for the insight I have figured out how I think I can accomplish this now and I am better for it! (also fixed my convention)
from random import randint
def card_deck():
#sets the card types and values
card_value = ['Ace','2','3','4','5','6','7','8','9','10','J','Q','K']
card_type = ['Hearts','Spades','Clubs','Diamonds']
deck = []
#This iterates all 52 cards into a deck
for i in card_type:
for j in card_value:
deck.append(j + ' of ' + i)
return deck
Now I can break this out into card and remove_card functions, everything is clicking!
def new_card(deck):
return deck[randint(0,len(deck)-1)]
def remove_card(deck,card):
return deck.remove(card)
new_deck = card_deck()
card1 = new_card(new_deck)
remove_card(new_deck,card1)
card2 = new_card(new_deck)
remove_card(new_deck,card2)
Upvotes: 0
Reputation: 14369
If looks like you are trying to get an item from a list and remove it from the list. There is the .pop()
method for this case:
card = Deck.pop()
This will get the last item in the list, assign it to card
and remove it from the list.
card = Deck.pop(1)
This will get the 2nd (index 1
) item from the list, assign it to card
and remove it from the list.
Upvotes: 2