Question: Blackjack part 2 , using the code below, add a function to transfer dealer and player wins and losses onto a csv file. import random
Blackjack part using the code below, add a function to transfer dealer and player wins and losses onto a csv file.
import random
class Card:
def initself suit, value:
self.suit suit
self.value value
def strself:
return fselfvalue of selfsuit
class Deck:
def initself:
self.cards
self.build
def buildself:
suits Hearts 'Diamonds', 'Clubs', 'Spades'
values 'Jack', 'Queen', 'King', 'Ace'
self.cards Cardsuit value for suit in suits for value in values
def shuffleself:
random.shuffleselfcards
def dealself:
return self.cards.pop
class Hand:
def initself:
self.cards
self.value
def addcardself card:
self.cards.appendcard
def calculatevalueself:
self.value
hasace False
for card in self.cards:
if card.value 'Ace':
hasace True
self.value self.cardvaluecard
if hasace and self.value :
self.value
return self.value
@staticmethod
def cardvaluecard:
if card.value in Jack 'Queen', 'King':
return
elif card.value 'Ace':
return
else:
return intcardvalue
def main:
# Initialize deck and players' hands
deck Deck
deck.shuffle
playerhand Hand
dealerhand Hand
# Deal initial cards
playerhand.addcarddeckdeal
dealerhand.addcarddeckdeal
playerhand.addcarddeckdeal
dealerhand.addcarddeckdeal
# Game loop
while True:
# Show player's hand and one of dealer's cards
print
Player's Hand:"
for card in playerhand.cards:
printcard
print
Dealer's Hand:"
printdealerhand.cards
printOne card face down."
# Ask player to hit or stand
try:
action input
Do you want to hit or stand
except EOFError:
print
End of input. Exiting the game."
break
if action.lowerh:
playerhand.addcarddeckdeal
playerhand.calculatevalue
if playerhand.value :
print
Player busts! Dealer wins."
break
elif action.lowers:
# Dealer's turn
while dealerhand.calculatevalue:
dealerhand.addcarddeckdeal
# Show dealer's hand
print
Dealer's Hand:"
for card in dealerhand.cards:
printcard
# Determine winner
if dealerhand.value :
print
Dealer busts! Player wins."
elif dealerhand.value playerhand.value:
print
Dealer wins."
elif dealerhand.value playerhand.value:
print
Player wins."
else:
print
It's a tie!"
break
if namemain:
main
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
