Python Forum
How to print the names of assignments if they are randomly selected
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to print the names of assignments if they are randomly selected
#1
Here is what I have (just playing around making a black jack game for a bit of Python practice):

from random import *
def main():
    aceHearts, aceSpades, aceDiamonds, aceClubs = 11, 11, 11, 11
    kingHearts, kingSpades, kingDiamonds, kingClubs = 10, 10, 10, 10
    queenHearts, queenSpades, queenDiamonds, queenClubs = 10, 10, 10, 10
    jackHearts, jackSpades, jackDiamonds, jackClubs = 10,10, 10, 10
    tenHearts, tenSpades, tenDiamonds, tenClubs = 10, 10, 10, 10
    nineHearts, nineSpades, nineDiamonds, nineClubs = 9, 9, 9, 9
    eightHearts, eightSpades, eightDiamonds, eightClubs = 8, 8, 8, 8
    sevenHearts, sevenSpades, sevenDiamonds, sevenClubs = 7, 7, 7, 7
    sixHearts, sixSpades, sixDiamonds, sixClubs = 6, 6, 6, 6
    fiveHearts, fiveSpades, fiveDiamonds, fiveClubs = 5, 5, 5, 5
    fourHearts, fourSpades, fourDiamonds, fourClubs = 4, 4, 4, 4
    threeHearts, threeSpades, threeDiamonds, threeClubs = 3, 3, 3, 3
    twoHearts, twoSpades, twoDiamonds, twoClubs = 2, 2, 2, 2

    deck = [
    aceHearts, aceSpades, aceDiamonds, aceClubs,
    kingHearts, kingSpades, kingDiamonds, kingClubs,
    queenHearts, queenSpades, queenDiamonds, queenClubs,
    jackHearts, jackSpades, jackDiamonds, jackClubs,
    tenHearts, tenSpades, tenDiamonds, tenClubs,
    nineHearts, nineSpades, nineDiamonds, nineClubs,
    eightHearts, eightSpades, eightDiamonds, eightClubs,
    sevenHearts, sevenSpades, sevenDiamonds, sevenClubs,
    sixHearts, sixSpades, sixDiamonds, sixClubs,
    fiveHearts, fiveSpades, fiveDiamonds, fiveClubs,
    fourHearts, fourSpades, fourDiamonds, fourClubs,
    threeHearts, threeSpades, threeDiamonds, threeClubs,
    twoHearts, twoSpades, twoDiamonds, twoClubs
            ]

    
    card1, card2 = sample(deck, 2)
Yes I understand there is probably a better way of doing this, but like I said I am just practicing as a new student of Python (and coding overall).

How would I be able to find the name of the card1 and card2 and how would I be able to print them?
Reply
#2
Quote:Yes I understand there is probably a better way of doing this, but like I said I am just practicing as a new student of Python (and coding overall). 

How would I be able to find the name of the card1 and card2 and how would I be able to print them?
The answer is still, like it or not, you wouldn't do it this way. Rarely do we create individual variables names for every element of a sequence. Instead you would use some combination of lists and dictionaries.

As an example of a possible approach:
import random


suits = ["Hearts", "Clubs", "Spades", "Diamonds"]
values = {"Two" : 2, 
          "Three" : 3,
          "Four" : 4,
          "Five" : 5,
          "Six" : 6,
          "Seven" : 7,
          "Eight" : 8,
          "Nine" : 9,
          "Ten" : 10,
          "Jack" : 10,
          "Queen" : 10,
          "King" : 10,
          "Ace" : 11}


deck = [(suit, value) for suit in suits for value in values]

pick_five = random.sample(deck, 5)
for s,v in pick_five:
    print("{} of {}\nValue: {}\n".format(v, s, values[v])) 
Output:
Jack of Clubs Value: 10 King of Clubs Value: 10 Seven of Spades Value: 7 Seven of Diamonds Value: 7 Ace of Clubs Value: 11
Reply
#3
at the moment you don't have names of the cards, you have 52 variables and a list with 52 integer numbers. this should be done in different way.
very basic approach, close to yours (i.e. there are better ways :-)):
 
from random import shuffle

cards = {'Two':2, 'Three':3, 'Four':4, 'Five':5, 'Six':6, 'Seven':7, 'Eight':8, 'Nine':9, 'Ten':10, 'Jack':10, 'Queen':10, 'King':10, 'Ace':11}
suits = ('Clubs', 'Diamonds', 'Hearts', 'Spades')
deck = [(card, suit, rank) for card, rank in cards.items() for suit in suits]
shuffle(deck) # shuffle the deck

card = deck.pop()
print("You've got {} of {}, value: {}".format(*card))
note that using sample is not correct as every time when you deal 2 cards, it will draw the sample from the full deck again
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyaudio seems to randomly halt input. elpidiovaldez5 2 311 Jan-22-2024, 09:07 AM
Last Post: elpidiovaldez5
  Problem with code / audio is playing randomly, not matching csv requirements Daniel_kcr 2 579 Sep-07-2023, 05:09 PM
Last Post: deanhystad
  Print names in x-axis of a time-series values hobbyist 4 1,177 Apr-22-2023, 09:29 PM
Last Post: deanhystad
  Where can I find Python training with reading, lecture, labs and assignments? davidorlow 2 917 Oct-20-2022, 10:46 AM
Last Post: rob101
  how to take a screnshot by Pyautogui automatically and randomly rachidel07 0 3,482 Feb-03-2021, 01:16 PM
Last Post: rachidel07
  assignments of function references Skaperen 3 2,314 Aug-15-2019, 11:12 AM
Last Post: fishhook
  module logging posting randomly DJ_Qu 2 2,187 May-14-2019, 06:41 AM
Last Post: DJ_Qu
  Can I search from Python, automatically and randomly generated keywords in Google? xX_Prophit_Xx 0 2,270 Sep-07-2018, 04:43 PM
Last Post: xX_Prophit_Xx
  Python seems to be randomly blowing up parmort 6 3,192 Aug-22-2018, 09:47 PM
Last Post: parmort
  Randomly changing numbers NicoConti 4 3,377 Mar-27-2018, 03:07 PM
Last Post: NicoConti

Forum Jump:

User Panel Messages

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