0
import random
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"]
suitvalues = [Diamonds, Hearts, Clubs, Spades]
potentialsuit = random.choice(suitvalues)
potentialcard = random.choice(potentialsuit)
print(potentialcard ,"of" ,potentialsuit.title)

My problem is that the potentialsuit.title part prints the entire list, whereas i only want to print the lists name. I am aware the part i wrote wont solve the issue, but thats just a substitute.

Froste
  • 13
  • 3

3 Answers3

6

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)
L3viathan
  • 26,748
  • 2
  • 58
  • 81
0

I don't know how you can print the name of a variable but I have two ideas that might help you:

  1. You might be able to find help in this other discussion.
  2. My recommendation -if your case is similar to the example you wrote- is that you use a Dictionary for that, same as L3viathan said.
sarafg11
  • 1
  • 1
0

This should solve it for you:

import random

face_card = ['King', 'Queen', 'Jack']
suit_card = ['Diamonds', 'Clubs', 'Spades', 'Hearts']
for x in range(1):
    choice = random.randint(1, 10)
    face_card.append(choice)  # Inserts integer randomly in to list
ran_fcard = random.choice(face_card)
ran_scard = random.choice(suit_card)

print('The card chosen is {} of {}'.format(ran_fcard, ran_scard))
face_card = ['King', 'Queen', 'Jack'] # Resets list to face cards

The output will be different every time and you will get your integers in there without the need for a long split select code.

These are the first 3 outputs I got:

The card chosen is 6 of Spades
The card chosen is King of Diamonds
The card chosen is 10 of Spades
Barb
  • 427
  • 3
  • 14