Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue with with list
#1
I was solving a challenge on Microsoft course on python, you are given output and asked to reproduce it.

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 code

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.....')
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?
Reply
#2
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.
BashBedlam and Abdirahman like this post
Reply
#3
If you useshuffle()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}')
Output:
There are 52 cards in deck. Dealing..... There are 47 in the deck Player has the follwoing cards in their hand: ['Jack of Spades', '8 of Clubs', '7 of Dimaonds', 'King of Dimaonds', '6 of Clubs']
Abdirahman likes this post
Reply
#4
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)]
Abdirahman likes this post
Reply
#5
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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020