Python Forum
Checking for one or more occurrence in a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Checking for one or more occurrence in a list
#1
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']
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#2
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.
Reply
#3
okay. I'll check in to that. Thanks much.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Checking if a string contains all or any elements of a list k1llcod3 1 1,081 Jan-29-2023, 04:34 AM
Last Post: deanhystad
  How to get unique entries in a list and the count of occurrence james2009 5 2,957 May-08-2022, 04:34 AM
Last Post: ndc85430
  Selecting the first occurrence of a duplicate knight2000 8 5,172 May-25-2021, 01:37 AM
Last Post: knight2000
  Checking number in a list to see if they are beside each other Allaye 5 5,849 Aug-26-2019, 07:15 AM
Last Post: perfringo
  Checking for an item in a list (if then statement) Sailnir 1 2,049 Jul-18-2019, 05:36 PM
Last Post: ndc85430
  count occurrence of numbers in a sequence and return corresponding value python_newbie09 6 3,441 May-20-2019, 06:33 PM
Last Post: python_newbie09
  Word co-occurrence matrix for a string (NLP) JoeB 2 11,600 Feb-27-2018, 11:21 PM
Last Post: Larz60+
  Maximum Occurrence Letter GalaxyCR 2 3,884 Nov-27-2017, 09:00 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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