Part II: Deal the Cards (10 points) Complete the function deal.cards, which take
ID: 3717507 • Letter: P
Question
Part II: Deal the Cards (10 points) Complete the function deal.cards, which takes one argument, deck, is a list Cards objects (but not necessarily exactly 52 cards). The objects in deck might appear in any random (shuffled) order. The function creates and returns (as a tuple) two Player objects with the following values: -player num: 1 or 2, as appropriate ..score: 0 .cards: the empty list The function appends alternating Card objects from deck to the_cards attribute of each Player object. More specifically, 1. Player 1 receives the Card object at deck [0] 2. Player 2 receives the Card object at deck [1 3. Player I receives the Card object at deck [2]. 4. Player 2 receives the Card object at deck [3]. and so on, for all the cards in deck.Explanation / Answer
ANswer :
Here is the deal_cards() function which takes deck as argument and return two player objects as tuples with values in them . Please refer the comments to understand what the code is doing.
def deal_cards(deck):
cards1=[] # declare empty card list for player 1
cards2=[] # declare empty card list for player 2
# iterate over deck and assign alternating card objects to each player
for index in range(0,len(deck),2):
cards1.append(deck[index]) # add card to player 1
if index+1<len(deck):
cards2.append(deck[index+1]) # add card to player
player1 = Player(1,0,cards1)
player2 = Player(2,0,cards2)
return player1 , player2