Hello,
First of all: I'm more used to java, but at university we need to code something in python. I'm having serious problems filling a python-list with values.

for i in range(self.num_of_players) :
     self.player_hands[i]=["Testcard"]

I want to fill the i-th player_hands-list with a String! Yes i declared a variable called player_hands in the classes init-method, looks like this:

self.player_hands = []

If above thing is an list, why isn't for loop doing its job ?

Im sorry: The interpreter error is: "list assignment index out of range".
And what i want to do is: Create a List for each player, which holds game-card values, like 7 or 2 or 3 etc..

Since there is no code on how or how many items in self.play_hands were declared, and no error message or what happened versus what was expected to happen, there is no way to comment. Generally, you would use list comprehension.

self.player_hands=[["Testcard"] for x in range(self.num_of_players)]

Also, "i" l" and "O" are not good choices for single digit variable names as they can look like numbers.

And see "3.2.2. Adding Elements to Lists" in this online book.

Since there is no code on how or how many items in self.play_hands were declared, and no error message or what happened versus what was expected to happen, there is no way to comment. Generally, you would use list comprehension.

self.player_hands=[["Testcard"] for x in range(self.num_of_players)]

Also, "i" l" and "O" are not good choices for single digit variable names as they can look like numbers.

Ah thanks. Can you re-edit your line of code to a more comprehendable form ? It works but i cannot extend it the way i want.

You can append, insert, or extend a list. See the link to the online book for more info, and here in the same book for list comprehensions. I have no idea what "a more comprehendable form" means.

Hello,
First of all: I'm more used to java, but at university we need to code something in python. I'm having serious problems filling a python-list with values.

for i in range(self.num_of_players) :
     self.player_hands[i]=["Testcard"]

I want to fill the i-th player_hands-list with a String! Yes i declared a variable called player_hands in the classes init-method, looks like this:

self.player_hands = []

If above thing is an list, why isn't for loop doing its job ?

Im sorry: The interpreter error is: "list assignment index out of range".
And what i want to do is: Create a List for each player, which holds game-card values, like 7 or 2 or 3 etc..

If get_card() is your card dealer function you can do (untested but about right):

number_of_players = 6
    cards_each = 5
    # initialize hand for each
    self.player_hands = [[get_card() for count in range(cards_each)]
                         for player_count in range(number_of_players)]

If get_card() is your card dealer function you can do (untested but about right):

number_of_players = 6
    cards_each = 5
    # initialize hand for each
    self.player_hands = [[get_card() for count in range(cards_each)]
                         for player_count in range(number_of_players)]

Hi tonyjv. This doesnt work for odd player numbers.
I've read the corresponding chapters in the book you gave me (and also searched through) other books. it seems to me that no function does what i want. What i want is exactly this:

self.player_hands = []
self.card_set = [(7),(7),(7),(7),(8),(8),(8),(8),(9),(9),(9),(9),(10),(10),(10),(10),\
        (11),(11),(11),(11),(12),(12),(12),(12),(14),(14),(14),(14),(13)]
self.num_of_players = 7

Each player hand should look like this after the loop (without shuffling the cards first, i know how to handle that. -> Random.shuffle())

self.player_hands = [[7,8,10,11,14],[7,8,10,11,14],[7,9,10,12,14],[7,9,10,12,14],[8,9,11,12,13],[8,9,11,12]]

I really tried everything, but nothing seems to work....

(7) = 7, why did you write like that? My code works, I tested it. How do you implement dealing, I usually use pop()

(7) = 7, why did you write like that? My code works, I tested it. How do you implement dealing, I usually use pop()

Ok then i rewrite, self.card_set without these brackets -> ()()() to just numbers.
Yes your code definitely works, but not for an odd player number like 3 or 5 or 7, since cards will be left in the card_set.
Right now i have to leave home aproximately at 11 o'clock i will be ready to post and test your working code against odd numbers. I had something similar already and it didnt work for odds.

Works for me, must be mistake in your dealer function like i said.

Works for me, must be mistake in your dealer function like i said.

My dealing implementation is simple: i just use

def get_card():
    self.card_set.pop()

, but a interpreter error occurs, stating: "IndexError: pop from empty list".

but card set is not empty:

self.card_set = [7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,\
        11,11,11,11,12,12,12,12,14,14,14,14,13]

I adapted your previously posted code snippet to(instead of just get_card, i added self.get_card, because i utilize a main function):

number_of_players = 6
            cards_each = 5
            # initialize hand for each
            self.player_hands = [[self.get_card() for count in range(cards_each)]
                         for player_count in range(number_of_players)]

But there ARE values in card_set. As you can see above...

I did like this, as I am missing your class:

class Cards(object):
    number_of_players = 6
    def __init__(self, cards_each = 5):
        self.cards_each = cards_each
        self.cards = [7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,11,11,11,11,12,12,12,12,14,14,14,14,13]

        # initialize hand for each
        self.player_hands = [[self.cards.pop() for count in range(cards_each) if self.cards]
                     for player_count in range(self.number_of_players)]
        
        print 'Initialized', self.player_hands, 'deck', self.cards

mycards=Cards()

It is poor naming to call card_set, when you have list. Call it deck or cards, please (the data type is irrelevant and can change according to implementation, read about duck typing)

Thanks mate, i again did some adaptions and it works now. If it bothers you, heres the code:(this evening im going to add funcionality to the card game, i may have another question to you)

self.player_hands = [[self.card_set.pop() for count in range(cards_each) if self.card_set]
                         for player_count in range(number_of_players)]
--------------------------------------------------------
def get_card(self):
        self.card_set = [7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,\
        11,11,11,11,12,12,12,12,14,14,14,14,13]       
        return self.card_set.pop()

EDIT:
UHM, but when dealing to 15 players each player has to recieve 2 cards and one player three. I need something that does deal "card by card" to "player by player" like:

self.card_set_pop() give this card to self.player_hands[0]
self.card_set_pop() give this card to self.player_hands[1]
self.card_set_pop() give this card to self.player_hands[2]
......

And when the end of the card_set is reached it should begin with player_hand[0] again until the last card is dealt.
Yes, im pretty demanding but im trying this thing for about 3 days now and it doesnt work.... (Although it sounds easy)

cards_each = int((round((len(self.card_set)/number_of_players),0)))

did its job...., you were also of great help tonyjw

That is impossible as cards in your list are 29, so each would get 2 except one only 1:

import random
class Cards(object):
    number_of_players = 15
    def __init__(self):
        # cards will be all delivered, so we do not need to make class variable
        cards = [7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,11,11,11,11,12,12,12,12,14,14,14,14,13]
        random.shuffle(cards)
        # initialize hand for each
        self.player_hands = [[] for count in (range(self.number_of_players))]
        for cardno, card in enumerate(cards):
            self.player_hands[cardno % self.number_of_players].append(card)
        
        print 'Initialized', len(self.player_hands), self.player_hands

mycards=Cards()

That is impossible as cards in your list are 29, so each would get 2 except one only 1:

import random
class Cards(object):
    number_of_players = 15
    def __init__(self):
        # cards will be all delivered, so we do not need to make class variable
        cards = [7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,11,11,11,11,12,12,12,12,14,14,14,14,13]
        random.shuffle(cards)
        # initialize hand for each
        self.player_hands = [[] for count in (range(self.number_of_players))]
        for cardno, card in enumerate(cards):
            self.player_hands[cardno % self.number_of_players].append(card)
        
        print 'Initialized', len(self.player_hands), self.player_hands

mycards=Cards()

Certainly it worked for any number of players but "11". I dont know why. But i could smash Python, if it was a person against the wall so that it becomes as plain as the wall is.

No not really, i did it totally different now, but i cant post it right now you have to wait until tomorrow, since i need to complete another university task for tomorrow.

Certainly it worked for any number of players but "11". I dont know why. But i could smash Python, if it was a person against the wall so that it becomes as plain as the wall is.

No not really, i did it totally different now, but i cant post it right now you have to wait until tomorrow, since i need to complete another university task for tomorrow.

this works the way i want it:

i = 1
            while len(self.card_set) > 0:
                a = random.randint(0, len(self.card_set)-1)
                if i <=int(self.num_of_players):
                    self.player_hands[i-1].insert(0, self.card_set[a])
                    i=i+1
                    if i == int(self.num_of_players)+1:
                        i=1
                self.card_set.remove(self.card_set[a])

            for hand in self.player_hands:
                    print(hand)

I need to add that the player_hands are generated with somtheing like:

for i in range(self.num_of_players):
    self.player_hands.insert(i,[])

Now i got the self.player_hands = [[],[],[],[],[],[],[],[],[],[],[],[],[],[]] filled only statically! Need to improve that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.