0

I want to write a for loop that make six decks of cards into variables but I cannot figure out how to write the code to determine what number the card is on.

for i in range(1, 313):
    for j in range(1, 53):
        for k in range(1, 5):
            if k == 1:
                color = "clubs"
            if k == 2:
                color = "diamond"
            if k == 3:
                color = "hearts"
            if k == 4:
                color = "spades"
            for y in range(1, 14):
                print(f"card_{i} = tuple(color = {color}, num = {y})")

It will print out 52 version of the card then go on to the next one instead I want it to write out 6 different deck of cards but all the cards have different variable name. Here is my desired output.

card_1 = tuple(color = clubs, num = 1)
#This would then go on till the 13th card then it would change to diamond.
card_12 = tuple(color = clubs, num = 13)
card_13 = tuple(color = diamond, num = 1)
card_14 = tuple(color = diamond, num = 2)
martineau
  • 119,623
  • 25
  • 170
  • 301
571Fanatic
  • 19
  • 1
  • Can you edit your question and show a snippet of what output you would like to get? – TMBailey Nov 27 '21 at 20:58
  • 1
    1- use `itertools.product`, 2- do not attempt to generate variables, use a dictionary – mozway Nov 27 '21 at 21:03
  • you don't need a loop dedicated to i, you just need a variable that increments... try looping in this order.... for each deck (6 times), for each rank (13 times) , for each suit (4 times).... to name the current card, just keep a counter in a variable that increments every inner loop – vicatcu Nov 27 '21 at 21:04

1 Answers1

0

There are several ways to approach this. I am gonna offer a couple of ideas. The first idea is probably most closely aligned with what you are describing/showing in your own code.

Approach 1: most similar to your code:

decks = {}
i = 0
for deck in range(6):
    for suit in ['clubs', 'diamond', 'hearts', 'spades']:
        for card in range(1, 14):
            i += 1
            print(f"card_{i} = tuple(color = {suit}, num = {card})")


card_1 = tuple(color = clubs, num = 1)
card_2 = tuple(color = clubs, num = 2)
...
card_12 = tuple(color = clubs, num = 12)
card_13 = tuple(color = clubs, num = 13)
card_14 = tuple(color = diamond, num = 1)
card_15 = tuple(color = diamond, num = 2)

Approach 2: uses itertools.product() to remove a for loop:

A potentially better approach that removes one layer of the nested for loops is to use the itertools.product() function.

from itertools import product

i = 0
for deck in range(6):
    for suit, num in product(['clubs', 'diamond', 'hearts', 'spades'], range(1,14)):
        i += 1
        print(f"card_{i} = tuple(color = {suit}, num = {num})")

Approach 3: Better storage of the results

Having said all of that, it is not clear to me whether you want real tuples stored in memory OR if you simply want to print something that looks like a tuple to the screen. Storing the data in a dictionary datatype would allow you to use that data later, if need be. Presuming that you want real tuples, the following code will store all the results of your work in some form of datatype, like a dictionary of tuples.

NOTE: tuples don't really store date using named arguments as you have shown above, they are more like lists where elements are simply stored in them, so below in the code, you will see us storing the element without any sort of named arguments:

tuple(suit, num) instead of tuple(color=suit, num=num)

Let's see how this works out.

from itertools import product

i = 0
decks = {}
for deck in range(6):
    for suit, num in product(['clubs', 'diamond', 'hearts', 'spades'], range(1,14)):
        i += 1

        # NOTE: here we are storing the data in a dictionary
        # NOTE: tuples don't store items using named values
        decks[f"card_{i}"] = tuple((suit, num))


# NOTE: here we use the items() method on our decks dictionary
# to extract each key and value in pairs so we can print them.
for card_num, card in decks.items():
    print(card_num, '=', card)


card_1 = ('clubs', 1)
card_2 = ('clubs', 2)
...
card_12 = ('clubs', 12)
card_13 = ('clubs', 13)
card_14 = ('diamond', 1)
card_15 = ('diamond', 2)
E. Ducateme
  • 4,028
  • 2
  • 20
  • 30
  • The OP want's to actually *create* variables with those names, not merely print out assignment statement that would do so if they were to somehow be executed. – martineau Nov 27 '21 at 22:31