Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help! Lists
#1
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'
Reply
#2
Please edit your post end put your code in formatter.
Reply
#3
When you built the deck you did this:
deck += [card]  # [card] is a list
When you dealt the cards you did this:
h += deck[f] # deck[f] is a string
To 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.
Reply
#4
(Apr-03-2020, 12:54 PM)deanhystad Wrote: When you built the deck you did this:
deck += [card]  # [card] is a list
When you dealt the cards you did this:
h += deck[f] # deck[f] is a string
To 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.

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']
Reply
#5
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.
Reply
#6
(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.
Reply
#7
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)
Reply
#8
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)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,312 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,190 Mar-20-2019, 08:01 PM
Last Post: stillsen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020