I'm trying to make a blackjack game as a beginners project. When I try to remove the card that's being dealt from the deck I get this: ValueError: list.remove(x): x not in the list. How can I fix this?
This is my code:
import random
deck = [[2, 2, 2, 2],
[3, 3, 3, 3],
[4, 4, 4, 4],
[5, 5, 5, 5],
[6, 6, 6, 6],
[7, 7, 7, 7],
[8, 8, 8, 8],
[9, 9, 9, 9],
[10, 10, 10, 10],
[10, 10, 10, 10],
[10, 10, 10, 10],
[10, 10, 10, 10],
[11, 11, 11, 11]
]
def deal_cards():
number = random.choice(deck[0:][0:]) # selecting the number of the card
card = random.choice(number) # selecting which suit from the number should be the card
new_deck = deck.remove(card) # Here is the problem
print(new_deck)
print(card)
deal_cards()
card is an int not list.That's why you are getting this error. Your deck contains list of lists. If you want to remove a single int then you should specify which list it should be removed. You should change your code as following:
def deal_cards():
number = random.choice(deck[0:][0:])# selecting the number of the card
card = random.choice(number)# selecting wich suit from the number
deck[deck.index(number)].remove(card) # problem fixed
print(deck) # remove returns nothing
print(card)