1

I'm trying to create a card dealing program but am having slight difficulty. My code looks like this:

cards = ["ace of spades", "ace of hearts", "2 of diamonds"] etc...
firstcard = random.choice(cards)
del cards[firstcard]
print(firstcard)

I am trying to remove the card that is chosen from the array and store it (so I can delete it for it not to be dealt twice) at the same time in order to print it.

I have been receiving this error: TypeError: list indices must be integers or slices, not str, however I'm not sure what other functions exist to help me.

Thanks!

Leo Cebon
  • 11
  • 1
  • 2
    Notice that `random.choice(cards)` returns an element from the cards list (not an index). For example, it might return `"ace of spades"` instead of `1`. – Joe Patten Sep 27 '18 at 18:51
  • You could do `cards.remove(firstcard)`, or use `pop` as suggested in @Austin's answer. See [Difference between del, remove and pop on lists](https://stackoverflow.com/questions/11520492/difference-between-del-remove-and-pop-on-lists) – Thierry Lathuille Sep 27 '18 at 19:00

4 Answers4

1

You could use random.randrange to randomly select item from list and list pop function to remove it from list:

import random

cards = ["ace of spades", "ace of hearts", "2 of diamonds"]

print(cards.pop(random.randrange(len(cards))))  # Randomly removed item.
print(cards)  # List after removal of the random item.
Austin
  • 25,759
  • 4
  • 25
  • 48
  • What I’m trying to do is print each card as chosen in order, as if a dealer is placing them 1 by 1. Is there a way to move the string directly and put it in another list called dealt cards? – Leo Cebon Sep 27 '18 at 19:36
  • Are you looking to store randomly selected items to another list in the order in which they are removed from the current list? – Austin Sep 28 '18 at 01:46
0

You might consider using random.sample(cards, len(cards)) to generate a new list that is in shuffled order. This would avoid changing the original cards list. See https://docs.python.org/dev/library/random.html#random.sample

sck26
  • 1
  • 1
0

So here I elaborated on your code. I sort of misread your question initially, but just added a simple fix by combining the final lists. This should return the cards to a new list in random order... if that is what you're after? In terms of dealing the cards out, you should be able to simply create a for loop and iterate through each card in the new list called shuffled_cards. Hope this helps, or at least part of it!

import random

cards = ["ace of spades", "ace of hearts", "2 of diamonds"] 
list_of_del_cards = []

i = 0

#while loop based on the number of cards in the list
while i < len(cards):

    selected_card = random.choice(cards) #randomly select card from list

    list_of_del_cards.append(selected_card) #append randomly selected card to a new list

    pos_in_list = cards.index(selected_card) #find pos of this card in old list

    del cards[pos_in_list] #del card from old list

    i += 1 #increment i each time to complete while loop

print(cards) #prints the list with the card that is left
print(list_of_del_cards) #print the list of deleted cards in order

shuffled_cards =  list_of_del_cards + cards

print(shuffled_cards)
Antonio
  • 77
  • 2
  • 13
0

"What I’m trying to do is print each card as chosen in order, as if a dealer is placing them 1 by 1. Is there a way to move the string directly and put it in another list called dealt cards?" - OP

This task can be done with or without using a extra list, but if your desire is to store the values if say this sample is larger and you are storing 12 out of 52, then I provided that solution as well.

Without extra list:

import random
cards = ["ace of spades", "ace of hearts", "2 of diamonds"]

while len(cards) > 0:
    print(cards.pop(random.randrange(len(cards))))

With extra list 'dealt_cards':

import random
dealt_cards = []

while len(cards) > 0:
    x = cards.pop(random.randrange(len(cards)))
    dealt_cards.append(x)
    print(x)
print('Dealt Cards: {}'.format(dealt_cards))
2 of diamonds
ace of hearts
ace of spades
Dealt Cards: ['2 of diamonds', 'ace of hearts', 'ace of spades']
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20