0

Im working on a program that creates a set of cards and then prints them, this is working fine so far. However I have been unable to figure out how to store each individual card in a way that a set number of cards, or individual cards, can be compaired to another set.

The function I'm using to "draw" the cards is:

def TableHand():
    print("Table Hand:")
    for i in range(0,5):
        print(RandomCardDraw(), "of", RandomHouseDraw())

The functions "RandomCardDraw" and "RandomHouseDraw" both just draw a random number and house.

I want to sort the final print result, for example - 7 of hearts, and then the next "card" in a list / dictionary or something that will let me compare the "table" to a user hand??

Idk if this is possible because of the way have coded my previous functions or not? if more information is needed to answer I can provide XD.

Thanks.

7
  • You want to sort the cards as the for loop generates them? If so what do you want to sort by, i.e. lexicographically, houses, numbers etc...? Commented Jun 28, 2017 at 10:01
  • How would you prevent drawing twice the same card if you pick randomly and separately the value and the suit? Commented Jun 28, 2017 at 10:02
  • @GaryDosSantos - My idea is that I want to compare the random card to another random card so I can then output different results depending on the cards - e.g. Highest card etc. Sorting by value would probably be better. Thanks. Commented Jun 29, 2017 at 7:51
  • @ThierryLathuille - Thats an issue I hadn't thought about! It hadn't occurred in the small amount of testing I'd done XD Thanks, I'm going to have to look further into sorting this issue. Commented Jun 29, 2017 at 7:53
  • You should create the complete deck of individual cards, and randomly choose one from it, excluding the already chosen ones as you go. Or you could keep your method and reject the cards it generates if they've already been chosen, which could be OK if you only draw a small number of them. Commented Jun 29, 2017 at 7:56

3 Answers 3

2

print is just for showing things, not for storing them. Here is how you can put all the cards (each represented by a tuple of two elements) into a set and retrieve it:

def TableHand():
    hand = set()
    for i in range(0, 5):
        card = (RandomCardDraw(), RandomHouseDraw())
        hand.add(card)
    return hand

(this is a good place for a set comprehension but I don't think you're ready for that yet)

Sign up to request clarification or add additional context in comments.

Comments

1

Alex Hall gives a good answer for providing a set, and I would like to expand on it for some specific data types you mentioned:

I want to sort the final print result, for example - 7 of hearts, and then the next "card" in a list / dictionary or something that will let me compare the "table" to a user hand??

List

If you just want to put your tuples in a list, do the following:

def TableHand():
    hand = []
    for i in range(0, 5):
        card = (RandomCardDraw(), RandomHouseDraw())
        hand.append(card)
    return hand

Lists are very easy to iterate over and perform various operations on. As Alex said, you may not be ready for set comprehension, so a list might be a more appropriate place to begin.

Dictionary

Storing the cards as a group in a dictionary is probably a bad way to go. Dictionaries are good fro representing things more like individual objects rather than a collection of objects. You really need to know how many elements you are going to get and have to have a standard way of naming them, so doing that dynamically is tedious and not really what dictionaries are designed for. However...

List of Dictionaries

Rather than storing the cards as tuples, you could do this:

def TableHand():
    hand = []
    for i in range(0, 5):
        card = { "number": RandomCardDraw(), "house": RandomHouseDraw())
        hand.append(card)
    return hand

NB: I've made an assumption about the names you'd like to give these elements, "number" and "house".

With a list of dictionaries you make comprehension and operations easier because what you're looking for becomes clearer to access. In this case, you could get the first card in the returned hand with first_card = hand[0], and you could then get the house (or whatever you name it as in the dictionary) with house = first_card["house"].

Sorting and Comparing

There are ways to sort lists and compare them against others - it's actually probably not even going to be that difficult, int your case. But the easiest way to do it is likely with set comprehension, in which case you should approach the problem with the solution Alex suggested.

3 Comments

I've had ago using the list and have managed to get it to work, however, when I print the final result it outputs the cards with all of the brackets and 'stuff'. Is there a way to stop this from happening or?? Thanks for the help :)
@JakeRosewell You tried it just as a list of tuples, like [(num, house), (num, house), ... ]? Then you will want to loop through the list with a for loop, like for card in hand, and you should do something like this: print(card[0], "of", card[1]), which will extract the first and second element from each tuple in the list to print them. I think it's clearer when done with a list of dictionaries, because it looks like this: print(card["number"], "of", card["house"]) and it shows more clearly what you want to access instead of just numbers. But it's a matter of personal preference.
Cheers for the advice, will look into doing it with in different ways and then decide the more effective method to solve the issue.
0

You can generate a deck of cards before hand that would make sorting easier and make sure you don't have any duplicate cards as that is a possibility if you generating them on the fly without checking first.

This is something I've done before, the suits variable contains d for diamonds, h for hearts and so on. The ranks are typed out in the same fashion. Now for the main part, the deck. The deck variable will be of type list where each item is a card in the deck. itertools.product() goes through ranks and suits and joins them together in a tuple in the variable item. .join() takes each item and joins the rank and suit together separating them with a dash.

suits = 'dhcs'
ranks = '234567890JQKA'
deck = []
for item in itertools.product(ranks, suits):
    '-'. join(item)
    deck.append(item)

To randomly select a card you can randomly select an item from deck and make sure to remove it.

Now the tricky part, sorting the cards. You can create a second function that assigns each card a value.

def giveValue(card):
    if card[2] == "A":
        return 14 
    if card[2] == "K":
        return 13

You can complete this function by going down the string and adding the value for each rank. This gives you numbers to sort each card by.

Hopefully this was of some use.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.