As usual, create a directory to hold today's files. All programs that you write today should be stored in this directory.
$ cd ~/cs120/labs $ mkdir lab24 $ cd lab24
Write a function called divide_string(a_string)
,
which
takes a string as a parameter. Your function should return a
string,
which is the second half of characters from a_string
followed by the
first half of characters.
Write a function called count_uppercase(a_string)
,
which takes a string as a parameter. This function should return
an integer, the number of upper case characters contained
within a_string.
If you don't know the card game blackjack, it's simple. Each player is dealt two cards. Face cards are worth 10 points, aces are worth 11 points, and all other cards are worth face value. You can hit (ask for another card), or you can stand (keep the cards you currently have). You can continue this process as long as you want. Once all players have stood, the player with the most points without going over 21 is the winner.
Create a Python functions
called deal_blackjack_hand(deck)
and
compute_blackjack_points(hand)
in the
file blackjack.py. The dealer function function should
take a list of strings, representing a deck of cards, and should
return a list of strings representing a single hand
of blackjack. This function should shuffle the deck, and "deal" the
top two cards into the players hand. The points function should
take a list of strings representing the players hands, and should
print the number of points the players hand contains.
>>> import random >>> deck = ['A♠', '2♠', '3♠', '4♠', '5♠', '6♠', '7♠', '8♠', '9♠', '10♠', 'J♠', 'Q♠', 'K♠', 'A♣', '2♣', '3♣', '4♣', '5♣', '6♣', '7♣', '8♣', '9♣', '10♣', 'J♣', 'Q♣', 'K♣', 'A♥', '2♥', '3♥', '4♥', '5♥', '6♥', '7♥', '8♥', '9♥', '10♥', 'J♥', 'Q♥', 'K♥', 'A♦', '2♦', '3♦', '4♦', '5♦', '6♦', '7♦', '8♦', '9♦', '10♦', 'J♦', 'Q♦', 'K♦'] >>> hand = deal_blackjack_hand(deck) >>> print(hand) [10♦, K♠] >>> points = compute_blackjack_points(hand) >>> print(points) 20
The player's hand should be represented as a list of
strings, just like the deck. It should be initially empty.
To deal a card to the player's hand, remove a card from the
end of the deck by using the pop
method and add
the card to the end of the player's hand by using
the append
method. You can use
random.shuffle(a_list)
.
The compute_blackjack_points
function should iterate over
each string in the list and use a string slice to copy the
rank of each card string. It should convert each rank
string into an integer and use an accumulator to compute the
sum of all of the ranks.
This version of blackjack is 100% dependent on luck. The way that skill becomes a factor in blackjack is in determining whether to hit or stay.
After the player is dealt 2 cards, enter a loop that allows the player to add new cards to their hand, until the user choses to stay.
>>> import random >>> deck = ['A♠', '2♠', '3♠', '4♠', '5♠', '6♠', '7♠', '8♠', '9♠', '10♠', 'J♠', 'Q♠', 'K♠', 'A♣', '2♣', '3♣', '4♣', '5♣', '6♣', '7♣', '8♣', '9♣', '10♣', 'J♣', 'Q♣', 'K♣', 'A♥', '2♥', '3♥', '4♥', '5♥', '6♥', '7♥', '8♥', '9♥', '10♥', 'J♥', 'Q♥', 'K♥', 'A♦', '2♦', '3♦', '4♦', '5♦', '6♦', '7♦', '8♦', '9♦', '10♦', 'J♦', 'Q♦', 'K♦'] >>> random.shuffle(deck) >>> play_blackjack_hand(deck) Your Hand: [10♦, K♠] Enter h to hit: h Your Hand: [10♦, K♠, 2♥] Enter h to hit: s You have 22 points.
This solitary version of Blackjack is kind of boring. Add a dealer to the mix. The game is still very similar. At the beginning, you will deal cards to both the player and the dealer. You should display one of the dealer's cards to the player, as is typical with a game of blackjack.
After the player hits until they are content, it is then the dealers turn. On the dealer's turn, repeatedly add cards to the dealer's hand until the total is 17 points or more.
After the dealer's turn, you should show the entirety of the dealers hand, display the total points for both the player and the dealer, and print who won
>>> import random >>> deck = ['A♠', '2♠', '3♠', '4♠', '5♠', '6♠', '7♠', '8♠', '9♠', '10♠', 'J♠', 'Q♠', 'K♠', 'A♣', '2♣', '3♣', '4♣', '5♣', '6♣', '7♣', '8♣', '9♣', '10♣', 'J♣', 'Q♣', 'K♣', 'A♥', '2♥', '3♥', '4♥', '5♥', '6♥', '7♥', '8♥', '9♥', '10♥', 'J♥', 'Q♥', 'K♥', 'A♦', '2♦', '3♦', '4♦', '5♦', '6♦', '7♦', '8♦', '9♦', '10♦', 'J♦', 'Q♦', 'K♦'] >>> random.shuffle(deck) >>> play_blackjack_hand(deck) Dealer's Hand: 2♠ ## ~~~~~Your Turn~~~~~ Your Hand: 10♦ K♠ Enter h to hit: h Your Hand: 10♦ K♠ 2♥ Enter h to hit: s ~~~Dealer's Turn~~~ Dealer hits. Dealer's Hand: 2♠ 10♣ 8♥ Dealer stays. You have 22 points. Dealer has 20 points. Dealer wins.