jeudi 9 novembre 2017

.popitem() selecting the exact same pairs from dictionary every time the code is run

I need to make a blackjack game between two computer players. I know how I'm going to do it, but I can't figure out this error Every time I assign card to be dealt to a player, it accumulates the total correctly but it draws the same cards every time.

code here

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}

hand_1 = 'string'
hand_1_value = 0
hand_1_total = 0

hand_2 = 'string'
hand_2_value = 0
hand_2_total = 0

while hand_1_total < 21:
    k, v = deck.popitem()
    hand_1 = k
    hand_1_total = hand_1_total + v

    print('Player 1 is dealt', hand_1)
    print('Player 1 score is',hand_1_total)

output here:

Player 1 is dealt King of Diamonds
Player 1 score is 10
Player 1 is dealt Queen of Diamonds
Player 1 score is 20
Player 1 is dealt Jack of Diamonds
Player 1 score is 30

Every time I run it, it assigns the royal suite of diamonds and I have no idea why? My textbook says that dict.popitem() returns a randomly selected key:value pair as a tuple to the variables declared.

My first thought was that being a tuple, the variable couldn't be changed ater the first iteration. But that doesn't sound right to me, nor does it explain why it's choosing the same cards each time.




Aucun commentaire:

Enregistrer un commentaire