简体   繁体   中英

Adding to a list each time a for loop iterates (Python)?

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.

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)

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM