Home > Back-end >  Creating random number and appending to list
Creating random number and appending to list

Time:12-04

populate each hand with two cards. Take a card from the deck and put it in the player_hand list. Then, take a card from the deck and put it in the dealer_hand list. Do that one more time in that order so that the dealer and player have each two cards. Make sure the dealer's first card is face down. I keep receiving this error from my 2 tests.

My code:

 while len(dealer_hand) != 2 and len(player_hand) != 2:
        player_card = random.choice(deck)
        player_hand.append(player_card)
        deck.remove(player_card)
        if len(player_hand) == 2:
            player_hand[0].face_up()
            player_hand[1].face_up()
        dealer_card = random.choice(deck)
        dealer_hand.append(dealer_card)
        deck.remove(dealer_card)
        if len(dealer_hand) == 2:
            dealer_hand[0].face_down()
            dealer_hand[1].face_up()

    return player_hand and dealer_hand


False != True

Expected :True
Actual   :False

def test_deal_cards():
        deck = []
        for suit in cards.SUITS:
            for rank in cards.RANKS:
                deck.append(cards.Card(suit, rank))
        dealer_hand = []
        player_hand = []
    
        blackjack.deal_cards(deck, dealer_hand, player_hand)
    
        assert len(dealer_hand) == 2
        assert len(player_hand) == 2
>       assert dealer_hand[0].is_face_up() is True
E       assert False is True
E           where False = <bound method Card.is_face_up of [10 of Hearts]>()
E             where <bound method Card.is_face_up of [10 of Hearts]> = [10 of Hearts].is_face_up

test_deal_cards.py:19: AssertionError





(test_deal_cards_alternates_between_player_and_dealer)
[7 of Hearts] != [6 of Spades]

Expected :[6 of Spades]
Actual   :[7 of Hearts]

def test_deal_cards_alternates_between_player_and_dealer():
        card1 = cards.Card(cards.SPADES, cards.SIX)
        card2 = cards.Card(cards.HEARTS, cards.SEVEN)
        card3 = cards.Card(cards.CLUBS, cards.EIGHT)
        card4 = cards.Card(cards.DIAMONDS, cards.NINE)
    
        deck = [card4, card3, card2, card1]
    
        dealer_hand = []
        player_hand = []
        blackjack.deal_cards(deck, dealer_hand, player_hand)
    
        assert len(dealer_hand) == 2
        assert len(player_hand) == 2
>       assert player_hand[0] is card1, 'Player 1st card should be Six of Spades'
E       AssertionError: Player 1st card should be Six of Spades
E       assert [7 of Hearts] is [6 of Spades]

test_deal_cards.py:39: AssertionError

CodePudding user response:

I think the error lies within the first line:

while len(dealer_hand) and len(player_hand) != 2:

If you want to check if the dealer also has 2 cards, you need to fix that line to:

while len(dealer_hand) != 2 and len(player_hand) != 2:

CodePudding user response:

First, the while loop is checking if the length of both the player and dealer hands are not equal to 2, which is not the correct condition. The while loop should continue as long as the length of either the player or dealer hand is less than 2, which can be checked using the 'or' operator.

Second, the code is using the 'card' attribute of the cards in the player and dealer hands, but it is not clear where this attribute is coming from. It is possible that the code is intended to use the 'face_up' and 'face_down' methods of the 'Card' class to set the visibility of the cards, but this is not properly implemented in the code.

Third, the code is not returning anything at the end, which is causing the test to fail. The code should return the player and dealer hands after they have been populated with two cards each.

Here is how the code could be fixed:

while len(dealer_hand) < 2 or len(player_hand) < 2:
    player_card = random.choice(deck)
    player_hand.append(player_card)
    deck.remove(player_card)
    if len(player_hand) == 2:
        player_hand[0].face_up()
        player_hand[1].face_up()

    dealer_card = random.choice(deck)
    dealer_hand.append(dealer_card)
    deck.remove(dealer_card)
    if len(dealer_hand) == 2:
        dealer_hand[0].face_down()
        dealer_hand[1].face_up()

return player_hand, dealer_hand

This code will draw two cards for each of the player and dealer hands, setting the first card of the dealer hand to be face down and the other cards to be face up. It will also return the player and dealer hands after they have been populated with the cards.

  • Related