Nitsuga Arashi - 1 year ago 55

Python Question

I've been struggling to figure out how to convert this program into one between two players instead of just the dealer and me. Is it supposed to loop somehow, giving the first iteration of the loop to player 1 and than the other to player 2? I'm not too sure how to make it work.

`def main():`

deck = create_deck()

num_cards = int(input('How many cards should I deal? '))

deal_cards(deck, num_cards)

def create_deck():

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 deck

def deal_cards(deck, number):

hand_value = 0

if number > len(deck):

number = len(deck)

for count in range(number):

card, value = deck.popitem()

print(card)

hand_value += value

print('Value of this hand:', hand_value)

main()

Answer Source

I suggest trying something like this:

```
import random
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}
def value(hand):
hand_value = 0
for i in hand:
hand_value += deck[i]
return hand_value
def main():
keys = list(deck)
random.shuffle(keys)
player1_hand = []
player2_hand = []
#dealing to player 1
while True:
print ("Player 1's hand is ", player1_hand, "The value is ", value(player1_hand))
response = int(input('How many cards should I deal to player1? '))
if response == 0:
break
else:
for i in range(int(response)):
player1_hand.append(keys.pop())
#dealing to player 2
while True:
print ("Player 2's hand is ", player2_hand, "The value is ", value(player2_hand))
response = int(input('How many cards should I deal to player2? '))
if response == 0:
break
else:
for i in range(int(response)):
player2_hand.append(keys.pop())
print (player1_hand, value(player1_hand))
print (player2_hand, value(player2_hand))
if __name__ == "__main__":
main()
```

Some changes: 1) The create_deck function is unnecessary because it just returns an unchanging variable, so it was removed. 2) Random.shuffle is used on the deck so that the game isn't always the same. 3) Added 'if name == main'. This is a simple formulation where player 1 gets as many cards as they want then player 2 does the same. You can loop around the dealing while loops, or write some separate function to specify the number of rounds. You could also try printing a lot of new line characters after a player finishes getting dealt cards so that the other player can't see what was dealt to them.