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


Messages In This Thread
Issue with with list - by Abdirahman - Jan-30-2022, 09:42 AM
RE: Issue with with list - by Yoriz - Jan-30-2022, 10:13 AM
RE: Issue with with list - by BashBedlam - Jan-30-2022, 01:43 PM
RE: Issue with with list - by deanhystad - Jan-30-2022, 02:41 PM
RE: Issue with with list - by Abdirahman - Jan-31-2022, 09:31 AM

Forum Jump:

User Panel Messages

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