How to print the names of assignments if they are randomly selected - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to print the names of assignments if they are randomly selected (/thread-5792.html) |
How to print the names of assignments if they are randomly selected - Kongurinn - Oct-22-2017 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? RE: How to print the names of assignments if they are randomly selected - Mekire - Oct-22-2017 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).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]))
RE: How to print the names of assignments if they are randomly selected - buran - Oct-22-2017 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 |