Question: This is Python. Is there any way I can change the following codes into Classes. Creating a card object here is my codes def main():
This is Python. Is there any way I can change the following codes into Classes. Creating a card object
here is my codes
def main(): # Create a deck of cards. deck = create_deck() # Deal the cards. deal_cards(deck) # The create_deck function returns a dictionary # representing a deck of cards. def create_deck(): # Create a dictionary with each card and its value # stored as key-value pairs. deck = {'Ace of Spades':1, '2 of Spades':2, '3 of Spades':3, '4 of Spades':4, '5 of Spades':5, '6 of Spades':6, '7 of Spades':7, '8 of Spades':8, '9 of Spades':9, '10 of Spades':10, 'Jack of Spades':10, 'Queen of Spades':10, 'King of Spades': 10, 'Ace of Hearts':1, '2 of Hearts':2, '3 of Hearts':3, '4 of Hearts':4, '5 of Hearts':5, '6 of Hearts':6, '7 of Hearts':7, '8 of Hearts':8, '9 of Hearts':9, '10 of Hearts':10, 'Jack of Hearts':10, 'Queen of Hearts':10, 'King of Hearts': 10, 'Ace of Clubs':1, '2 of Clubs':2, '3 of Clubs':3, '4 of Clubs':4, '5 of Clubs':5, '6 of Clubs':6, '7 of Clubs':7, '8 of Clubs':8, '9 of Clubs':9, '10 of Clubs':10, 'Jack of Clubs':10, 'Queen of Clubs':10, 'King of Clubs': 10, 'Ace of Diamonds':1, '2 of Diamonds':2, '3 of Diamonds':3, '4 of Diamonds':4, '5 of Diamonds':5, '6 of Diamonds':6, '7 of Diamonds':7, '8 of Diamonds':8, '9 of Diamonds':9, '10 of Diamonds':10, 'Jack of Diamonds':10, 'Queen of Diamonds':10, 'King of Diamonds': 10} # Return the deck. return deck # The deal_cards function deals a specified number of cards # from the deck. def deal_cards(deck): handValue = 0 p1 = 0 p2 = 0 while handValue <= 21: print("Player 1") card, value = deck.popitem() if len(deck) != 0: card, value = deck.popitem() if (card == "Ace of Spades " or card == "Ace of Hearts" or card == "Ace of Diamonds" or card == "Ace of Clubs"): if p1 + value < 21 : value += 10 print(card) p1 += value else: print("All cards have been dealt ") handValue = 21 print("Player 2") if len(deck) != 0: card, value = deck.popitem() if (card == "Ace of Spades " or card == "Ace of Hearts" or card == "Ace of Diamonds" or card == "Ace of Clubs"): if p1 + value < 21 : value += 10 print(card) p1 += value else: print("All cards have been dealt ") handValue = 21 if(p1 > p2) : handValue = p1 else: handValue = p2 if(p1 == p2): handValue = p1 print("Value of palyer 1\'s hand :", p1) print("Value of palyer 2\'s hand :", p2) if (p1 > p2): print("P1 wins!!!!") elif(p2 > p1): print("P2 Wins") else: "Neither of them wins !!!!" # Call the main function. main() Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
