65.5k views
3 votes
Write a game of Blackjack that functions as follows: Start by dealing two cards at random to the player. Store these cards and the associated values in separate lists. You should remove the two cards from the deck once they have been dealt to the player so they cannot be dealt a second time. Report the player's hand as well as the value of their hand. Next, as the player if they want to "hit" or "stand". "Hitting" means that the player wants another card. "Standing" means that the player is happy with their hand and doesn't want any more cards. If the player chooses to "hit" you should deal them another card (and remove it from the main deck). If their hand is worth 21 points they win the game. If their hand is over 21 points they lose the game (they went "bust"). If they have less than 21 points the process should be repeated (they should be re-prompted to see if they want to "hit" or "stand")

1 Answer

3 votes

Answer:

see explaination

Step-by-step explanation:

#Packages for random generation and exit

import random

import sys

#Main method

def main():

#Card list

cards=['10 of Hearts','9 of Hearts','8 of Hearts','7 of Hearts',

'6 of Hearts','5 of Hearts','4 of Hearts',

'3 of Hearts','2 of Hearts','Ace of Hearts','King of Hearts',

'Queen of Hearts','Jack of Hearts',

'10 of Diamonds','9 of Diamonds','8 of Diamonds','7 of Diamonds',

'6 of Diamonds','5 of Diamonds',

'4 of Diamonds','3 of Diamonds','2 of Diamonds','Ace of Diamonds','King of Diamonds',

'Queen of Diamonds','Jack of Diamonds','10 of Clubs','9 of Clubs','8 of Clubs',

'7 of Clubs','6 of Clubs','5 of Clubs','4 of Clubs',

'3 of Clubs','2 of Clubs','Ace of Clubs','King of Clubs',

'Queen of Clubs','Jack of Clubs','10 of Spades','9 of Spades','8 of Spades','7 of Spades',

'6 of Spades','5 of Spades','4 of Spades',

'3 of Spades','2 of Spades','Ace of Spades','King of Spades',

'Queen of Spades','Jack of Spades']

#VAlue list

values=[10,9,8,7,6,5,4,3,2,1,10,10,10,10,9,8,7,6,5,4,3,2,1,10,10,10,10,9,8,7,6,5,4,3,2,1,10,10,10,

10,9,8,7,6,5,4,3,2,1,10,10,10]

#Declare player cards

playerCards=[]

playerScore=0

n=random.randint(0,52)

playerCards.append(cards[n])

playerScore+=values[n]

n=random.randint(0,52)

playerCards.append(cards[n])

playerScore+=values[n]

computerCards=[]

computerScore=0

ch="h"

print(playerCards,"is worth ",playerScore)

#Loop for player play

while(ch=='h'):

playerScore=player(cards,values,playerCards,playerScore)

if(playerScore>21):

print('computer wins!')

sys.exit()

elif(playerScore==21):

print("Player wins!!!")

sys.exit()

else:

ch=input("(h)it or (s)tand? ")

#Computer list initialization

n=random.randint(0,52)

computerCards.append(cards[n])

computerScore+=values[n]

n=random.randint(0,52)

computerCards.append(cards[n])

computerScore+=values[n]

print(computerCards,"is worth ",computerScore)

#Computer loop

while(computerScore<playerScore and computerScore<21):

computerScore+=computer(cards,values,playerCards,playerScore)

if(computerScore>21 ):

print("Player wins!!!")

sys.exit()

elif(computerScore==21 or computerScore>playerScore):

print("Computer Wins!!")

sys.exit()

#Player method

def player(cards,values,playerCards,playerScore):

n=random.randint(0,52)

print('You drew a card of ',cards[n])

playerCards.append(cards[n]);

playerScore+=values[n]

print(playerCards,"is worth ",playerScore)

return playerScore;

#computer method

def computer(cards,values,computerCards,computerScore):

n=random.randint(0,52)

print('Computer drew a card of ',cards[n])

computerCards.append(cards[n]);

computerScore+=values[n]

print(computerCards,"is worth ",computerScore)

return computerScore;

#Program starts here

if __name__== "__main__":

main()

User Brendon Muir
by
3.7k points