Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3610184
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:43:14+00:00 2026-05-18T21:43:14+00:00

So I’m stuck on a newbie problem once more :D I’m trying to mash

  • 0

So I’m stuck on a newbie problem once more 😀

I’m trying to mash together a text based game of go-fish against the computer.

Ok so 1 card is actually a tuple of elements from two lists.

suits = ['Clubs', 'Diamonds', 'Spade', 'Hearts']
ranks = [None, 'ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'jack', 'queen', 'king']

Then that is added to a deck and shuffled and whatnot and then dealt into hands. (got most of it from the book thinkpython i think. Learned a lot about class structure and inheritance in the process.)

So one hand might look like this

['Clubs 2', 'Diamonds king', 'Diamonds 2', 'Spades 2', 'Hearts 2']

As you can see that hand contains four of the same rank so that’s 1 point for the player.
But how would I check if the hand contains four instances of any item in the ranks list?
Do I have to iterate through each item in the list or there some clean and simple way to it?


EDIT
Thanks a lot for all the answers guys. 😀
But I’m getting an attribute error when I try to use ‘split’ on the items in the hand.
Guess I should have posted more of the code I’m running.

Full code and traceback here
http://pastebin.com/TwHkrbED

Is there something wrong with how the methods are defined in Card?
I’ve been hacking around for hours trying to make it work, but no luck.

EDIT2
Made some changes to the deck generating part. Now the whole deck is a list of tuples and a lot less code.

thedeck=[]
class Deckofcards:
    suits = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
    ranks = ['Ace', '2', '3', '4', '5', 
        '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']
    def __init__(self):
        for i in self.suits:
            for a in self.ranks:
                thedeck.append((i, a))

Seems like the other way was overly complicated, but idk. I’ll see how it goes tomorrow, adding the actual game parts.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T21:43:14+00:00Added an answer on May 18, 2026 at 9:43 pm

    I might suggest a slight refactoring: Represent each card in a hand as a tuple of (rank, suit). So your example hand would be:

    hand = [('2', 'Clubs'),
            ('king', 'Diamonds'),
            ('2', 'Diamonds'),
            ('2', 'Spades'),
            ('2', 'Hearts')]
    

    Then I’d suggest a couple of auxiliary functions to help you determine the value of a hand:

    from collections import defaultdict
    
    def get_counts(hand):
        """Returns a dict mapping card ranks to counts in the given hand."""
        counts = defaultdict(int)
        for rank, suit in hand:
            counts[rank] += 1
        return counts
    
    def get_points(hand):
        """Returns the number of points (ie, ranks with all 4 cards) in the given
        hand."""
        return sum(1 for count in get_counts(hand).itervalues() if count == 4)
    

    Edit: Switched to using sum in the get_points function, which seems clearer to me.

    Working with these functions and the example hand you gave, you get output like so:

    >>> get_counts(hand)
    defaultdict(<type 'int'>, {'king': 1, '2': 4})
    
    >>> get_points(hand)
    1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.