Question
Project 2: Blackjack with Dictionaries
You saw the card_dealer.py program that simulates cards being dealt from a deck. We are going to enhance this program so it simulates a simplified version of the game of Blackjack between two virtual players. The cards have the following values:
- Numeric cards are assigned the value they have printed on them. For example, the value of the 2 of spades is 2, and the value of the 5 of diamonds is 5.
- Jacks, queens, and kings are valued at 10.
- Aces are valued at 1 or 11, depending on the player’s choice.

The program should deal cards arbitrarily to each player until one player’s hand is worth more than 21 points. When that happens, the other player is the winner. (It is possible that both players’ hands will simultaneously exceed 21 points, in which case neither player wins.) If a player is dealt an ace, the program should decide the value of the card according to the following rule: The ace will be worth 11 points, unless that makes the player’s hand exceed 21 points. In that case, the ace will be worth 1 point.
You have been provided with starter code blackjack_starter.py on Canvas.
You will need to complete the sections #TODO in comments as follows:
- Complete the main() function by:
#TODO - Deal a card to each player and calculate hand value.
#Print 'Player 1 was dealt...'
#Print 'Player 2 was dealt...'
#TODO Determine the winner.
#Print either:
#Print 'There is no winner' or
#'Player 1 wins' or
#'Player 2 wins'

- Complete the create_deck() function by:
#TODO Add the numbers 2-10 to the deck [Hint: you will need to check if the value is numeric]
#TODO Add the Ace, King, Queen, or Jack values to the deck

- Complete the update_hand_value(hand, value, card) function by:
# TODO Given the player's current hand, the value of the card they were just dealt
# and the name of the card they were just dealt, return the new value of their hand
# Remember: If a player is dealt an ace, the program should decide the value by:
# The ace will be worth 11 points, uless that makes the player's hand exceed 21 points.
# In that case the ace will be worth 1 point.

It is recommended that you print out data structures such as the deck to understand how they work. Often times in the real world, you will start off with code that is already written and part of your job will be understanding it and then adding to it. It is important you have a clear understanding of how the deck is stored in the dictionary and what the key is and what the value is.

Extra Credit:
For the main question you only need to deal cards arbitrarily. For extra credit, deal the cards randomly from the deck.
Solution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references.
Students may use these solutions for personal skill-building and practice.
Unethical use is strictly forbidden.

# Blackjack code

import random

# Global constant for the winning number of cards
MAX = 21

# main function
def main():
    # Local variables
    hand1 = 0
    hand2 = 0
    deck = create_deck()
    roundNum = 1

    while True:
       print('Round', roundNum)

       cards = list(deck.keys())
       random.shuffle(cards)
      
       #TODO - Deal a card to each player and calculate hand value.
       #Print 'Player 1 was dealt...'
       card1 = cards[0]
       value1 = deck[card1]
       del deck[card1]
       suit1, num1 = card1
       print('Player 1 was dealt', num1, 'of', suit1)
       hand1 = update_hand_value(hand1, value1, card1)
      
      
       #Print 'Player 2 was dealt...'
       card2 = cards[1]
       value2 = deck[card2]
       del deck[card2]
       suit2, num2 = card2
       print('Player 2 was dealt', num2, 'of', suit2)
       hand2 = update_hand_value(hand2, value2, card2)

       print('Player
This is only a preview of the solution.
Please use the purchase button to see the entire solution.
By purchasing this solution you'll be able to access the following files:
Solution.PNG
Solution.py
Purchase Solution
$36.00
Google Pay
Amazon
Paypal
Mastercard
Visacard
Discover
Amex
View Available Computer Science Tutors 641 tutors matched
Ionut
(ionut)
Master of Computer Science
Hi! MSc Applied Informatics & Computer Science Engineer. Practical experience in many CS & IT branches.Research work & homework
5/5 (6,804+ sessions)
1 hour avg response
$15-$50 hourly rate
Pranay
(math1983)
Doctor of Philosophy (PhD)
Ph.D. in mathematics and working as an Assistant Professor in University. I can provide help in mathematics, statistics and allied areas.
4.6/5 (6,688+ sessions)
1 hour avg response
$40-$50 hourly rate
Leo
(Leo)
Doctor of Philosophy (PhD)
Hi! I have been a professor in New York and taught in a math department and in an applied math department.
4.9/5 (6,435+ sessions)
2 hours avg response

Similar Homework Solutions