Python Forum
Blackjack total from string HW
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Blackjack total from string HW
#1
So I've got an assignment that i need help with the game Blackjack on python.
I was given a list to work with (not allowed to change it either) and already have the functions for dealing the cards.
At the moment i am having trouble trying to convert the dealt cards (outputs as strings) into an number.

Here is my list for the deck of cards from a py file 'playing_bj':
List = ['AH','2H','3H','4H','5H','6H','7H','8H','9H','TH','JH','QH','KH',
        'AD','2D','3D','4D','5D','6D','7D','8D','9D','TD','JD','QD','KD',
        'AS','2S','3S','4S','5S','6S','7S','8S','9S','TS','JS','QS','KS',
        'AC','2C','3C','4C','5C','6C','7C','8C','9C','TC','JC','QC','KC']
 
Currently this is what i have:
import playing_bj
hand = []
# Deals starting card to player
card = playing_bj.deal_one_card()
#Adds dealt card to hand list
hand.append(card)
#Deal second card
card = playing_bj.deal_one_card()
#Adds second dealt card to hand list
hand.append(card)
#Display player's hand
print (hand)
At the moment the hand displays as [AH, 3S] but i'm required to display it in text format along with the total, eg: (Player Hand Total is 4/14 with Ace of Hearts and 3 of Spades)
So i am really just focusing on displaying the total from the dealt cards, along with its suites.
I feel that this is something very simple that i'm not seeing, i would really appreciate if anyone could guide me to the right direction and help me with how i can go about doing this.
Reply
#2
All you have to do is to convert the numbers by using str() function to strings and then concatenate them by using

python 2.x
num = 4
maxnum = 14
spades = 3
print "Player Hand Total is "+str(num)+"/"+str(maxnum)+" with Ace of Hearts and "+str(spades)+" of Spades"
python 3.x
num = 4
maxnum = 14
spades = 3
print("Player Hand Total is "+str(num)+"/"+str(maxnum)+" with Ace of Hearts and "+str(spades)+" of Spades")
BUT in your case i would use something like:
( you dont need to use str() because it will already be given as a string )
lst = ['AH','3S']

# python 2.7
print "Player Hand Total is "+(lst[1][0])+" Spades"

# python 3.x
print("Player Hand Total is "+(lst[1][0])+" Spades")
in this case [1] is for accessing the second item which is 3S and [1][0] is for accessing the first string-element of 3Swhich is '3'
Reply
#3
(Apr-27-2017, 10:06 AM)ichistarr Wrote: At the moment the hand displays as [AH, 3S] but i'm required to display it in text format along with the total, eg: (Player Hand Total is 4/14 with Ace of Hearts and 3 of Spades)
It's not an so easy task as you are given a list(that you can not change) as shall display that as output.
Can give some hint.
Could map the list values onto to a dictionary.
Eg.
>>> cards = {'3S': {'3 of Spades': 3}, 'AH': {'Ace of Hearts': 11}}
>>> my_hand = ['AH', '3S']
>>> cards_info = []
>>> for value in my_hand:
...     if value in cards:
...         cards_info.append(cards[value])
... 
>>> cards_info
[{'Ace of Hearts': 11}, {'3 of Spades': 3}]
So now have list with text and values of my_hand.
>>> # Display
>>> print('{} - {}'.format(''.join(cards_info[0]), ''.join(cards_info[1])))
Ace of Hearts - 3 of Spades

>>> # Calculate
>>> def clac(cards_info):     
...     for item in cards_info:
...         for card in item:
...             yield item[card]
            
>>> total = sum(dislay(cards_info))
>>> print('Card value {}/{}'.format(total-10, total))
Card value 4/14
@idontreallywolf look into string formatting,stuff like this is not nice +str(num)+"/"+str(maxnum)+.
Reply
#4
@snippsat Define "not nice"
Reply
#5
(Apr-27-2017, 04:47 PM)idontreallywolf Wrote: @snippsat Define "not nice"
num = 4
maxnum = 14
spades = 3

# Not nice
print("Player Hand Total is "+str(num)+"/"+str(maxnum)+" with Ace of Hearts and "+str(spades)+" of Spades")

# String formattting {} was new in Python 2.6
print("Player Hand Total is {}/{} with Ace of Hearts and {} of Spades".format(num,maxnum,spades))

# New in Python 3.6 f-string it's what we all be using in future
print(f"Player Hand Total is {num}/{maxnum} with Ace of Hearts and {spades} of Spades")
Output:
Player Hand Total is 4/14 with Ace of Hearts and 3 of Spades Player Hand Total is 4/14 with Ace of Hearts and 3 of Spades Player Hand Total is 4/14 with Ace of Hearts and 3 of Spades
Reply
#6
Maybe try using a dict to keep track of what the options are?

>>> cards = {
...   "A": {"label": "Ace", "values": [1, 14]},
...   "K": {"label": "King", "values": [13]},
...   "Q": {"label": "Queen", "values": [12]},
...   "J": {"label": "Jack", "values": [11]}
... }
>>> for i in range(2, 11):
...   cards[str(i)] = {"label": str(i), "values": [i]}
...
>>> pprint.pprint(cards)
{'10': {'label': '10', 'values': [10]},
 '2': {'label': '2', 'values': [2]},
 '3': {'label': '3', 'values': [3]},
 '4': {'label': '4', 'values': [4]},
 '5': {'label': '5', 'values': [5]},
 '6': {'label': '6', 'values': [6]},
 '7': {'label': '7', 'values': [7]},
 '8': {'label': '8', 'values': [8]},
 '9': {'label': '9', 'values': [9]},
 'A': {'label': 'Ace', 'values': [1, 14]},
 'J': {'label': 'Jack', 'values': [11]},
 'K': {'label': 'King', 'values': [13]},
 'Q': {'label': 'Queen', 'values': [12]}}
Then you just build a hand out of the keys of that dict, and when displaying it to the user, you use the "label" value to show Ace/Jack, and the "values" value to know if you need to calculate split hand values.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Blackjack unionjackandsally 4 2,566 Apr-27-2020, 08:39 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020