'Comparing the Suite and Value of Two Random Cards in Python

I am working on a program that will select two random cards from a deck (our instructor is having us use a dictionary to symbolize the deck) to see if they share the same suite (hearts, spades, etc.) or the same value (Aces, 2-10, J, Q, K). Keep in mind we haven't been taught Object-Oriented Programming yet so my knowledge on those concepts is limited. This is the code I have so far but unfortunately do not know where to go from here. How should I continue from here? Keep in mind I am using PYTHON.

import random 

def deck():
i = 0
suite = ['\u0003','\u0004','\u0005','\u0006']
nu = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
deck = []
total_count = []
for c in suite:
    for j in range(len(nu)):
        i += 1
        card = nu[j] + c
        deck.append(card)
        total_count.append(i)
deck_dictionary = dict(zip(deck, total_count))
print(deck, '\n\n')
print(deck_dictionary)
card1= random.choice(list(deck_dictionary.values())) 
card2 = random.choice(list(deck_dictionary.values()))


Solution 1:[1]

You defined the suits as:

    suite = ['\u0003','\u0004','\u0005','\u0006']

But were probably after something like:

    suit = ['\u2665', '\u2666', '\u2663', '\u2660']

I assume all the code after the def is the body of your function, and you call it from somewhere. However, if the function is really just the main program, there's not that much point in calling it as a function (unless it was something reusable, but it's clearly just the main program).

Having a function that generates a new deck might make sense though. However your code introduces a lot of extra variables and loops that aren't really doing all that much. It's unclear why you want to create a dictionary that maps each card to an initial position or unique number, but if that's what you need, something like this is enough:

def deck():
    suit = ['\u2665', '\u2666', '\u2663', '\u2660']
    value = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
    return {c: i for i, c in enumerate(f'{v}{s}' for s in suit for v in value)}

Or, if you want to stay closer to your code:

def deck():
    suit = ['\u2665', '\u2666', '\u2663', '\u2660']
    value = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
    i = 0
    result = {}
    for c in suit:
        for v in value:
            i += 1
            result[f'{v}{c}'] = i
    return result

The rest of your program doesn't really use the fact that you defined a dictionary like that and can't really take advantage of it, but you can still fairly easily get the list of cards, pick 2 at random, compare the suits and values and decide if they are the same:

deck_dictionary = deck()
print(deck_dictionary)

deck_list = list(deck_dictionary.keys())
card1 = random.choice(deck_list)
card2 = random.choice(deck_list)

print(f'picked {card1} and {card2}')
print('same suit' if card1[1] == card2[1] else 'not the same suit')
print('same value' if card1[0] == card2[0] else 'not the same value')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Grismar