Home > database >  Python homework not working - receving an error
Python homework not working - receving an error

Time:10-30

I am trying to write this code but I Receve the following error and I am not sure exactly why it is not printing out.

The assigment is to make sure that whenever we pull out for example Ace of Spades, Ace of Hearts, Ace of Diamonds and Ace of Clubs they are not to be in the [my_cards] section.

I tried the following code:

import random

standard_cards = [2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A"]
my_cards = {"Hearts": standard_cards, "Diamonds": standard_cards, "Clubs": standard_cards, "Spades": standard_cards}
new_cards = list(my_cards)
picked_cards = []

for card in range(3):
    random.shuffle(my_cards)
    chosen_card_color = random.choice(my_cards)
    chosen_card = random.choice(chosen_card_color)
    my_cards.remove(chosen_card)
    picked_cards.append(chosen_card_color   " "   chosen_card)

print("User took these 3 cards:\r\n", picked_cards)

Here is the error I receive:

"C:\Python lectures\TestovProekt\venv\Scripts\python.exe" "C:/Python lectures/TestovProekt/Domashno.py"
Traceback (most recent call last):
  File "C:\Python lectures\TestovProekt\Domashno.py", line 19, in <module>
    random.shuffle(my_cards)
  File "C:\Users\BOP\AppData\Local\Programs\Python\Python39\lib\random.py", line 362, in shuffle
    x[i], x[j] = x[j], x[i]
KeyError: 0

Process finished with exit code 1

Any help would be really appreciated!

CodePudding user response:

As noted in The_spider's answer, there are a number of issues with your original code. Their answer addresses those issues, so I will propose a different solution using a list of card tuples:

import random
import itertools


ranks = [2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A"]
suits = ["Spades", "Hearts", "Clubs", "Diamonds"]
cards = list(itertools.product(ranks, suits))

deck = cards.copy()
random.shuffle(deck)

user_hand = [deck.pop() for _ in range(3)]

Output:

[(3, 'Diamonds'), ('J', 'Spades'), (10, 'Clubs')]

From here, it'd be pretty straight-forward to use a dictionary to store "players", and deal new hands to all the players in the correct order (instead of 3 cards at once for each player which is not how poker hands are dealt).

CodePudding user response:

mycards is a dictionary, which you can't shuffle. As you want to choose a card color here, I think you intended to chose from new_cards, which does contain the 4 card colors. This also requires that you've to get the card itself with a dictionary key. Also, first shuffling and then choosing random appears me a bit useless, you can't concatenate strings and int and, as Random Davis said, you should copy your standard_list. Otherwise, the cards will be removed from every color, and not only from the one you just chosen, meaning that you'll never get 2 the same card of a different type.

The entire loop should look something like this:

import random

standard_cards = [2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A"]
my_cards = {"Hearts": standard_cards.copy(), "Diamonds": standard_cards.copy(), "Clubs": standard_cards.copy(), "Spades": standard_cards}
#you can leave one of them uncopied, as it won't affect the other ones anymore now.
new_cards = list(my_cards)
picked_cards = []

for card in range(3):
    chosen_card_color = random.choice(new_cards)
    chosen_card = random.choice(my_cards[chosen_card_color])
    my_cards[chosen_card_color].remove(chosen_card)
    picked_cards.append(chosen_card_color   " "   str(chosen_card))

print("User took these 3 cards:\r\n", picked_cards)
  • Related