Jan-30-2022, 09:42 AM
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 the given output:
Output:There are 52 cards in the deck.
Dealing ...
There are 47 cards in the deck.
Player has the following cards in their hand:
['Jack of Hearts', 'Queen of Hearts', '4 of Spades', 'Ace of Hearts', '9 of Diamonds']
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:
Error:Traceback (most recent call last):
File "C:\Users\Ahmed\source\repos\pythonlist\pythonlist\Challenge.py", line 16, in <module>
deck.remove(card)
ValueError: list.remove(x): x not in list
Press any key to continue . . .
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?