Practice Problem 1
Write a function double_all(a_list)
, which takes a list
of floating point numbers. Your function should return a version
of a_list with all values doubled.
Practice Problem 2
Write a function remove_all(a_list, an_element)
, which
takes a list and some element. Your function should remove all
occurances of an_element from a_list, and return the
resulting list.
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.
Details
Create a Python function
called play_blackjack_hand(deck)
in the
file blackjack.py. The function should play a single hand
of blackjack. The function's parameter deck
represents
a deck of cards as a list of strings. To play a hand of blackjack
do the following:
-
Deal two cards from the deck to the player's hand (also a list of strings).
-
Repeatedly prompt the user if they want to hit.
-
When the player is done hitting, print the total points of the player's hand.
It is difficult to test a program that uses user input like this one. Think about what functions you could write to make writing the program easier and more testable.
Example
>>> 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.
Hint
-
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 theappend
method. -
Add a while loop that runs while the user has entered 'h'. Inside the loop, deal one more card to the user's hand, print the hand, and prompt the user to hit again.
-
Create a function
hand_points(hand)
that returns the sum of all of the cards in the the listhand
. The 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. Test this function before using it in theplay_blackjack_hand
function.
Challenge
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.
Challenge
By counting cards you can know when you have a higher probability of winning a hand. When you have a higher probability of winning you can bet more money. Unless you are a savant, counting all the cards difficult. Instead, it's not too difficult to estimate how many high cards are left in the deck by using a hi-lo system. Every card in the deck is given a value.
Card Value | Hi-lo Value |
2 – 6 | -1 |
7 – 9 | 0 |
10 – A | 1 |
When the sum of the hi-lo value of the dealt cards is low, there is a higher probability that the next card dealt will be a 10 through ace. Modify your blackjack program to also print the hi-lo card count every time a card is dealt.
>>> 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♠ ## Hi-lo: -1 ~~~~~Your Turn~~~~~ Your Hand: 10♦ Hi-lo: 0 Your Hand: 10♦ K♠ Hi-lo: 1 Enter h to hit: h Your Hand: 10♦ K♠ 2♥ Hi-lo: 0 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.
Submission
Please show your source code and run your programs for the instructor or lab assistant. Only a programs that have perfect style and flawless functionality will be accepted as complete.