I am currently working on a texas hold'em poker game project and got stuck with my new card dealing class:
import random
cardnames = ['2C', '2D', '2H', '2S', '3C', '3D', '3H', '3S',
'4C', '4D', '4H', '4S', '5C', '5D', '5H', '5S',
'6C', '6D', '6H', '6S', '7C', '7D', '7H', '7S',
'8C', '8D', '8H', '8S', '9C', '9D', '9H', '9S',
'10C', '10D', '10H', '10S', '11C', '11D', '11H', '11S',
'12C', '12D', '12H', '12S', '13C', '13D', '13H', '13S',
'14C', '14D', '14H', '14S']
class Card_Dealer():
def __init__(self):
self.cards = cardnames
self.cards_available = cardnames.copy()
self.dealing_to_hands()
self.dealing_to_table()
def dealing_to_hands(self):
self.playercards = []
self.p1_cards = []
self.p2_cards = []
self.p3_cards = []
self.p4_cards = []
hands = [self.playercards, self.p1_cards,
self.p2_cards, self.p3_cards,
self.p4_cards]
for hand in hands:
card = random.choice(self.cards_available)
hand[0][1] = card
hand[0][0] = self.cards.index(card)
self.cards_available.remove(card)
card = random.choice(self.cards_available)
hand[1][1] = card
hand[1][0] = self.cards.index(card)
self.cards_available.remove(card)
def dealing_to_table(self):
self.cardsontable = {1: None, 2: None, 3: None, 4: None, 5: None}
for a in self.cardsontable:
card_name = random.choice(self.cards_available)
card_ind = self.cards.index(card_name)
card = (card_ind, card_name)
self.cardsontable[a] = card
self.cards_available.remove(card_name)
My first problem is, that when I try to call a players cards (e.g. p1_cards) I get this error message:
Traceback (most recent call last):
File "C:/Users/Buza Dani/Documents/Programming/Projects/Texas_Holdem/poker_cards.py", line 229, in <module>
print(Card_Dealer().p1_cards)
File "C:/Users/Buza Dani/Documents/Programming/Projects/Texas_Holdem/poker_cards.py", line 195, in __init__
self.dealing_to_hands()
File "C:/Users/Buza Dani/Documents/Programming/Projects/Texas_Holdem/poker_cards.py", line 211, in dealing_to_hands
hand[0][1] = card
IndexError: list index out of range
The second problem is, that if I will be able to fix the first problem, each time, I'll call a variable from the class, they will change because during the initialization the lists will grow and because of the random, the actual variables, from which I call one, will be different every time.
I need some help fixing these problems with some explanation, since I'm a very beginner.