Help! Lists - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Help! Lists (/thread-25566.html) |
Help! Lists - bwdu - Apr-03-2020 Hi everybody! This is my code; import stdio import sys import random SUITS = ["Clubs", "Diamonds", "Hearts", "Spades"] RANKS = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen ", "King", "Ace"] rank = random . randrange (0 , len( RANKS )) suit = random . randrange (0 , len( SUITS )) stdio . writeln ( RANKS [ rank ] + "of" + SUITS [ suit ]) deck = [] for rank in RANKS : for suit in SUITS : card = rank + "of" + suit deck += [ card ] n = len ( deck ) for i in range ( n ): r = random . randrange (i , n) temp = deck [r] deck [r] = deck [i] deck [i] = temp print(deck) h = [] b = int(sys.argv[1]) k = 1 for l in range(b): while k <= b: f = random.randrange(n) h += deck[f] k += 1 print(h) ['Queen ofClubs', '7ofClubs', '4ofDiamonds', 'AceofDiamonds', '8ofSpades', '5ofDiamonds', 'KingofHearts', 'Queen ofDiamonds', 'KingofDiamonds', '2ofDiamonds', '10ofDiamonds', 'JackofSpades', 'JackofClubs', '4ofSpades', 'Queen ofHearts', '10ofClubs', 'JackofDiamonds', '5ofClubs', 'KingofSpades', '8ofClubs', '5ofHearts', 'JackofHearts', '10ofSpades', 'Queen ofSpades', '8ofDiamonds', 'AceofSpades', '3ofSpades', 'AceofClubs', 'AceofHearts', '4ofHearts', '2ofHearts', '3ofClubs', 'KingofClubs', '2ofSpades', '4ofClubs', '6ofDiamonds', '9ofDiamonds', '7ofSpades', '8ofHearts', '9ofSpades', '9ofHearts', '3ofDiamonds', '6ofClubs', '2ofClubs', '6ofHearts', '9ofClubs', '7ofHearts', '10ofHearts', '7ofDiamonds', '5ofSpades', '3ofHearts', '6ofSpades'] ['2', 'o', 'f', 'C', 'l', 'u', 'b', 's', '9', 'o', 'f', 'C', 'l', 'u', 'b', 's', 'Q', 'u', 'e', 'e', 'n', ' ', 'o', 'f', 'S', 'p', 'a', 'd', 'e', 's', '6', 'o', 'f', 'D', 'i', 'a', 'm', 'o', 'n', 'd', 's', '3', 'o', 'f', 'S', 'p', 'a', 'd', 'e', 's'] ['2', 'o', 'f', 'C', 'l', 'u', 'b', 's', '9', 'o', 'f', 'C', 'l', 'u', 'b', 's', 'Q', 'u', 'e', 'e', 'n', ' ', 'o', 'f', 'S', 'p', 'a', 'd', 'e', 's', '6', 'o', 'f', 'D', 'i', 'a', 'm', 'o', 'n', 'd', 's', '3', 'o', 'f', 'S', 'p', 'a', 'd', 'e', 's'] ['2', 'o', 'f', 'C', 'l', 'u', 'b', 's', '9', 'o', 'f', 'C', 'l', 'u', 'b', 's', 'Q', 'u', 'e', 'e', 'n', ' ', 'o', 'f', 'S', 'p', 'a', 'd', 'e', 's', '6', 'o', 'f', 'D', 'i', 'a', 'm', 'o', 'n', 'd', 's', '3', 'o', 'f', 'S', 'p', 'a', 'd', 'e', 's'] ['2', 'o', 'f', 'C', 'l', 'u', 'b', 's', '9', 'o', 'f', 'C', 'l', 'u', 'b', 's', 'Q', 'u', 'e', 'e', 'n', ' ', 'o', 'f', 'S', 'p', 'a', 'd', 'e', 's', '6', 'o', 'f', 'D', 'i', 'a', 'm', 'o', 'n', 'd', 's', '3', 'o', 'f', 'S', 'p', 'a', 'd', 'e', 's'] ['2', 'o', 'f', 'C', 'l', 'u', 'b', 's', '9', 'o', 'f', 'C', 'l', 'u', 'b', 's', 'Q', 'u', 'e', 'e', 'n', ' ', 'o', 'f', 'S', 'p', 'a', 'd', 'e', 's', '6', 'o', 'f', 'D', 'i', 'a', 'm', 'o', 'n', 'd', 's', '3', 'o', 'f', 'S', 'p', 'a', 'd', 'e', 's'] I have created 5 lists(h) with second print command. How can i make the elements like previous list? for example, first list should look like; ["2ofclubs", "9ofclubs", "queenofspades","6ofdiomands","3ofspapdes"] !NOTE: .split() function gives error: print(h.split( )) AttributeError: 'list' object has no attribute 'split' RE: Help! Lists - Mateusz - Apr-03-2020 Please edit your post end put your code in formatter. RE: Help! Lists - deanhystad - Apr-03-2020 When you built the deck you did this: deck += [card] # [card] is a listWhen you dealt the cards you did this: h += deck[f] # deck[f] is a stringTo prevent appending a bunch of strings together you can do like you did when making the cards or you can use list.append() h += [deck[f]] or h.append(deck[f])I think you should build the your deck and hands using "append()", it is more familiar to programmers. RE: Help! Lists - bwdu - Apr-03-2020 (Apr-03-2020, 12:54 PM)deanhystad Wrote: When you built the deck you did this: Okey i did what you said and it helped a lot, thank you. The output has occured this way, but i am trying to create five different decks for five players, i think i have a problem in the last loop. What can i do for that ?(By the way i only changed deck[f] to [deck[f]]) ['10ofClubs', '8ofSpades', '10ofSpades', 'Queen ofDiamonds', '3ofSpades'] ['10ofClubs', '8ofSpades', '10ofSpades', 'Queen ofDiamonds', '3ofSpades'] ['10ofClubs', '8ofSpades', '10ofSpades', 'Queen ofDiamonds', '3ofSpades'] ['10ofClubs', '8ofSpades', '10ofSpades', 'Queen ofDiamonds', '3ofSpades'] ['10ofClubs', '8ofSpades', '10ofSpades', 'Queen ofDiamonds', '3ofSpades'] RE: Help! Lists - deanhystad - Apr-03-2020 You will need a different hand for each player, and when you deal a card you need to remove it from the deck so you don't deal the same card again. RE: Help! Lists - bwdu - Apr-04-2020 (Apr-03-2020, 06:28 PM)deanhystad Wrote: You will need a different hand for each player, and when you deal a card you need to remove it from the deck so you don't deal the same card again.Yes i generated that idea too, i couldn't create the code of this logic. RE: Help! Lists - deanhystad - Apr-04-2020 This code uses a class to represent the deck of cards. You can do this with functions, but a class packages the cards and the functions so nicely. import random class CardDeck: """A deck of cards""" ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King', 'Ace'] suits = ['Clubs', 'Diamonds', 'Hearts', 'Spades'] def __init__(self): """Make a deck of cards""" self.cards = [] for suit in self.suits: for rank in self.ranks: self.cards.append(f'{rank} of {suit}') self.topcard = 0 def shuffle(self): """Shuffle cards to be in random order""" random.shuffle(self.cards) self.topcard = 0 def deal(self, numcards): """Deal a hand of numcards""" numcards = min(numcards, len(self)) # Don't deal too many cards hand = self.cards[self.topcard:self.topcard+numcards] self.topcard += numcards return hand def __len__(self): """Return how many cards remaining in deck""" return len(self.cards) - self.topcard deck = CardDeck() deck.shuffle() hands = [deck.deal(5) for _ in range(4)] # Deal 4 hands of 5 cards print(len(deck), 'cards remaining in deck') print('Hands') for hand in hands: print(hand) RE: Help! Lists - perfringo - Apr-05-2020 Another way (requires 3.8 <= Python, otherwise assigning 'hand' should be on separate row): from random import sample from itertools import product suits = ["Clubs", "Diamonds", "Hearts", "Spades"] ranks = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"] deck = {' of '.join(card) for card in product(ranks, suits)} hands = list() for i in range(5): deck = deck.difference(hand := sample(deck, 5)) hands.append(hand) |