How can I make it print ace for 1, jack for 11, queen for 12 and king for 13 while playing the game

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How can I make it print ace for 1, jack for 11, queen for 12 and king for 13 while playing the game



How can I make it print ace instead of 1, jack instead of 11, queen instead of 12 and king instead of 13, while playing the game, with the python program still working properly.



Is there a way I can do this without changing too much.



This is my code:


import random

dealer_cards =
player_cards =

while len(dealer_cards) != 2:
dealer_cards.append(random.randint(1, 13))
if len(dealer_cards) == 2:
print('The dealer has a hidden card and', dealer_cards[1])

while len(player_cards) != 2:
player_cards.append(random.randint(1, 11))
if len(player_cards) == 2:
print('You have the cards,', player_cards)

if sum(dealer_cards) == 21:
print('The dealer has the cards,', dealer_cards)
print('The dealer has won because he has 21!')
exit()

if sum(player_cards) == 21 and sum(dealer_cards) == 21:
print('draw')
exit()

elif sum(dealer_cards) > 21:
print('The dealer has the cards,', dealer_cards)
print('The dealer has bust because he has over 21!')
exit()

while sum(player_cards) < 21:
choice = str(input('Choose twist or stick? '))
if choice == 'twist':
player_cards.append(random.randint(1, 11))
print('You now have the cards,', player_cards)
else:
print('The dealer has the cards,', dealer_cards)
print('You have the cards,', player_cards)
if sum(dealer_cards) > sum(player_cards):
print('The dealer has won!')
break
else:
print('You have won!')
break

if sum(player_cards) > 21:
print('You have bust because you are over 21!')

elif sum(player_cards) == 21:
print('You have won because you have 21')





Hard-code a dict that maps integer values to the string you want printed for each. See other card-game installations if you need specific details. 1:"ace", 11:"jack", ...
– Prune
Aug 10 at 17:47






Have you considered making a Card class with a __str__ method?
– Fred Larson
Aug 10 at 17:47


Card


__str__




1 Answer
1



You can make a dict of names and use it to extract the name:


card_names =
1: 'Ace',
11: 'Jack',
12: 'Queen',
13: 'King',


>>> n = 12
>>> print(card_names.get(n, n))
Queen



The second parameter to get makes sure that if a number is not found in the dict it is printed as it is.


get



Now, your code is printing lists, that makes it more complex. You have to query that dict for each element of the list.



Your code:


print('You now have the cards,', player_cards)



Becomes:


print('You now have the cards: ',
', '.join(str(card_names.get(c, c)) for c in player_cards))



That will iterate over the cards and query the dict one by one. Do that every time you're printing a list and it will work.



Since you have to repeat that snippet every time you're printing a list, you can create a function to avoid repetition:


def format_card_list(card_list):
return ', '.join(str(card_names.get(c, c)) for c in card_list))



Then use it in your code:


print('You now have the cards: ', format_card_list(player_cards))
...
print('The dealer has the cards,', format_card_list(dealer_cards))
...



etc





Thanks, I’ll try it.
– Electropoliton
Aug 11 at 15:42






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS