Python Forum

Full Version: Checking for one or more occurrence in a list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm going to try my luck with a simple 5 card stud poker game but, I've ran into a roadblock.
What is going to be the best way to compare the cards.
I'm speculating that I'm going too have to do a split to have access to both left and right of a card element.
Any guidance is much appreciated.

#! /usr/bin/env python3

import random as rnd


card_types = ['Spades', 'Hearts', 'Diamonds', 'Clubs']
cards = ['Ace', 2, 3, 4, 5, 6, 7, 8 , 9, 10, 'Jack', 'Queen', 'King']

deck = []
hand = []
for card in cards:
    for card_type in card_types:
        deck.append(f'{card} {card_type}')

rnd.shuffle(deck)

for i in range(5):
    hand.append(deck.pop())

print(hand)
Output:
['8 Diamonds', '6 Spades', '8 Spades', 'King Clubs', 'King Hearts']
If you have, or can get your hands on a copy of Fluent Python, there's a very elegant way to define card decks with examples of various operations such as shuffle, keep track of what's been played, and probably some comparisons as well.
Worth a look if you can get your hands on a copy.
okay. I'll check in to that. Thanks much.
I did a Texas hold'm in python.
This is how i defined the deck(s)
deck = []
d = ['A','2','3','4','5','6','7','8','9','X','J','Q','K']
t = ['H','S','D','C']

for card in d:
    for f in t:
        deck.append(f + card)           
random.shuffle(deck)
The harder part is determining who won :-)
(I do have a copy of Fluent Python, but i did not know about the card game.
I'll have a look later on)
Paul