In my Rummy program I have to order the output so that the cards are printed with high to low value (K being the highest, A being the lowest) for example:
input: 1. 8H, 8C, 8S, 2C, 7S, 9H, KD
output: 1. 8S, 8H, 8C, KD, 9C, 7H, 2C
run4 = []
set4 = []
run3 = []
set3 = []
other = []
cards = input('1. ')
cards = cards.split(', ')
card_numbers_count = {'A':0, '2':0, '3':0, '4':0, '5':0, '6':0, '7':0, '8':0, '9':0, 'T':0, 'J':0, 'Q':0, 'K':0}
card_suit_count = {'H':0, 'D':0, 'S':0, 'C':0}
def card_suit_key(card):
suit_vals = {'S': 1, 'H': 2, 'C': 3, 'D': 4}
return suit_vals[card[1]]
def numerical_order(card):
num_vals = {'K': 1, 'Q': 2, 'J': 3, 'T': 4, '9': 5, '8': 6, '7': 7, '6': 8, '5': 9, '4': 10, '3': 11, '2': 12, 'A': 13}
return num_vals[card[1]]
# card value
for card in cards:
card_number = card[:-1]
card_numbers_count[card_number] += 1
for card in cards:
card_number = card[:-1]
if card_numbers_count[card_number] == 3:
run3.append(card)
run3.sort(key=card_suit_key)
elif card_numbers_count[card_number] == 4:
run4.append(card)
run4.sort(key=card_suit_key)
elif card_numbers_count[card_number] > 3 or card_numbers_count[card_number] < 3:
other.append(card)
print (other)
#other.sort(key=numerical_order)
print (run4, run3, other)
# card suit
for card in cards:
card_suit = card[1:]
card_suit_count[card_suit] += 1
for card in cards:
card_suit = card[1:]
I have edited the question because I was able to do it for ordering the suits from highest to lowest but I am not able to do the same for card value. Any help would be appreciated. Thanks.