That doesn't work, because lists (like any other Python objects) have no name.
Imagine the following scenario:
x = [1, 2, 3]
y = x
y
, which is not just a copy of x
, but refers to the same list (you can see that by asking x is y
), is just as valid a name for the list as x
. So which name should ....title
choose?
One of many solutions of your problem is storing your cards in a dictionary:
import random
suits = {
"Diamonds": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King"],
"Hearts": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King"],
"Clubs": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King"],
"Spades": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King"],
}
potentialsuit = random.choice(list(suits))
potentialcard = random.choice(suits[potentialsuit])
print(potentialcard, "of", potentialsuit)
list(suits)
exploits the fact that iterating over a dictionary yields its keys (the suits). potentialsuit
will then not be a list of card values, but rather the name of the suit, e.g. "Clubs". The second choice then chooses one of suits["Clubs"]
, which is the list of card values.
Come to think of it, it doesn't really make sense to choose a random card like that; you don't need four copies of the list. Instead, the following suffices:
import random
suit = random.choice(["Diamonds", "Hearts", "Clubs", "Spades"])
value = random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King"])
print(value, "of", suit)