{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "___\n", "\n", "\n", "___\n", "
while
loops. This is a common practice used to control the flow of the game.**\n",
"\n",
" suits = ('Hearts', 'Diamonds', 'Spades', 'Clubs')\n",
" ranks = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')\n",
" values = {'Two':2, 'Three':3, 'Four':4, 'Five':5, 'Six':6, 'Seven':7, 'Eight':8, 'Nine':9, 'Ten':10, 'Jack':10,\n",
" 'Queen':10, 'King':10, 'Ace':11}"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import random\n",
"\n",
"suits = ('Hearts', 'Diamonds', 'Spades', 'Clubs')\n",
"ranks = ('Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace')\n",
"values = {'Two':2, 'Three':3, 'Four':4, 'Five':5, 'Six':6, 'Seven':7, 'Eight':8, 'Nine':9, 'Ten':10, 'Jack':10,\n",
" 'Queen':10, 'King':10, 'Ace':11}\n",
"\n",
"playing = True"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Class Definitions\n",
"Consider making a Card class where each Card object has a suit and a rank, then a Deck class to hold all 52 Card objects, and can be shuffled, and finally a Hand class that holds those Cards that have been dealt to each player from the Deck."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Step 2: Create a Card Class**try
/except
. Remember to check that a Player's bet can be covered by their available chips."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def take_bet(chips):\n",
" \n",
" while True:\n",
" try:\n",
" chips.bet = int(input('How many chips would you like to bet? '))\n",
" except ValueError:\n",
" print('Sorry, a bet must be an integer!')\n",
" else:\n",
" if chips.bet > chips.total:\n",
" print(\"Sorry, your bet can't exceed\",chips.total)\n",
" else:\n",
" break"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We used a while
loop here to continually prompt the user for input until we received an integer value that was within the Player's betting limit."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A QUICK NOTE ABOUT FUNCTIONS:while
loop later on in our code."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def hit_or_stand(deck,hand):\n",
" global playing # to control an upcoming while loop\n",
" \n",
" while True:\n",
" x = input(\"Would you like to Hit or Stand? Enter 'h' or 's' \")\n",
" \n",
" if x[0].lower() == 'h':\n",
" hit(deck,hand) # hit() function defined above\n",
"\n",
" elif x[0].lower() == 's':\n",
" print(\"Player stands. Dealer is playing.\")\n",
" playing = False\n",
"\n",
" else:\n",
" print(\"Sorry, please try again.\")\n",
" continue\n",
" break"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Step 9: Write functions to display cards***
symbol is used to print every item in a collection, and the sep='\\n '
argument prints each item on a separate line.\n",
"\n",
"* In the fourth line where we have\n",
"\n",
" print('',dealer.cards[1])\n",
" \n",
" the empty string and comma are there just to add a space.\n",
"\n",
"- Here we used commas to separate the objects being printed in each line. If you want to concatenate strings using the +
symbol, then you have to call each Card object's \\_\\_str\\_\\_ method explicitly, as with\n",
"\n",
" print(' ' + dealer.cards[1].__str__())\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Step 10: Write functions to handle end of game scenarios**