Issue with with list - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Issue with with list (/thread-36227.html) |
Issue with with list - Abdirahman - Jan-30-2022 I was solving a challenge on Microsoft course on python, you are given output and asked to reproduce it. here is the given output: here is my codesuits = ['Hearts','Spades','Clubs','Dimaonds'] ranks = ['2','3','4','5','6','7','8','9','10','Jack','Queen','King','Ace'] deck = [] for suit in suits: for rank in ranks: deck.append(f'{rank} of {suit}') deck_number = len(deck) player_hand= [] print(f'There are {deck_number} cards in deck.') print('Dealing.....') import random card=random.choices(deck,k=5) deck.remove(card) player_hand=append.card deck_number=len(deck) print(f'There are {deck_number} in the deck') print(f'Player has the follwoing cards in their hand:\n {player_hand}')Here is error: here is solution to their challenge :import random suits = ["Hearts", "Spades", "Clubs", "Diamonds"] ranks = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"] deck = [] for suit in suits: for rank in ranks: deck.append(f'{rank} of {suit}') print(f'There are {len(deck)} cards in the deck.') print('Dealing ...') hand = [] while len(hand) < 5: card = random.choice(deck) deck.remove(card) hand.append(card) print(f'There are {len(deck)} cards in the deck.') print('Player has the following cards in their hand:') print(hand)I have tried their code and it worked, why it does not work with my code? is it becuse they used the while function and I did not? RE: Issue with with list - Yoriz - Jan-30-2022 card=random.choices(deck,k=5)returns a list of strings deck.remove(card)You are trying to remove that list of strings which is not an item of the list. Their code is dealing with only one card string at a time in a loop. RE: Issue with with list - BashBedlam - Jan-30-2022 If you use shuffle() instead ofrandom.choice() then you can just slice the deck like this.suits = ['Hearts','Spades','Clubs','Dimaonds'] ranks = ['2','3','4','5','6','7','8','9','10','Jack','Queen','King','Ace'] deck = [] for suit in suits: for rank in ranks: deck.append(f'{rank} of {suit}') deck_number = len(deck) player_hand= [] print(f'There are {deck_number} cards in deck.') print('Dealing.....') from random import shuffle shuffle(deck) player_hand=deck[-5:] deck=deck[:-5] deck_number=len(deck) print(f'There are {deck_number} in the deck') print(f'Player has the follwoing cards in their hand:\n {player_hand}')
RE: Issue with with list - deanhystad - Jan-30-2022 A few comments: Imports belong at the top of the file, not buried in the code. Why do you repeat line 7? Better yet, why do you have "deck_number"? If you want to know how many cards remain in the deck, use len(deck). This removes writing extra code that needs to be maintained and tested. Making a deck of cards can be done with a very pleasing list comprehension. deck = [f"{rank} of {suit}" for suit in suits for rank in ranks]As already mentioned, shuffling the deck is definitely the best way to go. Dealing can be done by popping or slicing. random.shuffle(deck) hand = deck[:5] deck = deck[5:] # or hand = [deck.pop() for _ in range(5)] # Dealing from the bottom!The pop() method modifies the existing deck. The slice method creates a new deck I you use choice() you need to remove the cards from the deck. Either removing the cards or building a new deck that doesn't contain the delt cards hand = random.choices(deck, k=5) for card in hand: deck.remove(card) # This was the problem in your code. Needed to call remove for each card, not list of cards # or deck = [card for card in deck if not card in hand]The remove() method modifies the existing deck. The list comprehension method creates a new deck minus the cards delt. You could also randomly pop cards out of the deck. hand = [deck.pop(random.randint(0, len(deck)-1)) for _ in range(5)] RE: Issue with with list - Abdirahman - Jan-31-2022 Thank you all for the help, I was not introduced to the shuffle method yet but it seems it is the best way to slice through a list. |