How can i use this code (below) to take the random numbers from the dice roll and change 6 to "A" and 5 to "K" and ect.

#Poker Dice! Noah Sutton 4/24/10
import random

rounds=0
#roll dice
def roll_dice():
	#Dice variables
	dice1=random.randint(1,6)
	dice2=random.randint(1,6)
	dice3=random.randint(1,6)
	dice4=random.randint(1,6)
	dice5=random.randint(1,6)
	'1'=9
	'2'=10
	'3'=J
	'4'=Q
	'5'=K
	'6'=A
	print "You rolled "+str(dice1)+","+str(dice2)+","+str(dice3)+","+str(dice4)+","+str(dice5) 
		

#Implement the code:
roll_dice()

Recommended Answers

All 6 Replies

Here is a session with the python interpreter which should help you:

Python 2.6.4
>>> values = "9 10 J Q K A".split()
>>> print(values)
['9', '10', 'J', 'Q', 'K', 'A']
>>> print list(enumerate(values))
[(0, '9'), (1, '10'), (2, 'J'), (3, 'Q'), (4, 'K'), (5, 'A')]
>>> print [(i+1, v) for (i, v) in enumerate(values)]
[(1, '9'), (2, '10'), (3, 'J'), (4, 'Q'), (5, 'K'), (6, 'A')]
>>> mapping = dict((i+1, v) for (i, v) in enumerate(values))
>>> mapping
{1: '9', 2: '10', 3: 'J', 4: 'Q', 5: 'K', 6: 'A'}
>>> mapping[3]
'J'
>>> mapping[6]
'A'
>>> import random
>>> mapping[random.randint(1, 6)]
'Q'
>>> mapping[random.randint(1, 6)]
'9'
>>> rolls = [mapping[randint(1,6)] for i in range(5)]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'randint' is not defined
>>> rolls = [mapping[random.randint(1,6)] for i in range(5)]
>>> rolls
['10', 'A', 'A', '10', '10']
>>> rolls
['10', 'A', 'A', '10', '10']
>>> rolls = [mapping[random.randint(1,6)] for i in range(5)]
>>> print rolls
['9', 'Q', '9', '10', 'A']
>>> print ", ".join(rolls)
9, Q, 9, 10, A
>>> print "You rolled " + ", ".join(rolls)
You rolled 9, Q, 9, 10, A
>>>

How can i use this code (below) to take the random numbers from the dice roll and change 6 to "A" and 5 to "K" and ect.

Use a Python dictionary...or two.

#!/usr/bin/env python

import sys, random

dice_to_cards = { 1:'9', 2:'10', 3:'J', 4:'Q', 5:'K', 6:'A' }
hand = { 1:'', 2:'', 3:'', 4:'', 5:'' }

# play() - Play the game.
def play():
    num = 1
    hand = { 1:'', 2:'', 3:'', 4:'', 5:'' } # reset values for each run, just to be sure.
    while num <= 5:
        dice = random.randint(1,6)
        hand[num] = dice_to_cards[dice]
        num += 1
        pass

    print '%-5s - %5s' % ('Dice #', 'Value')
    dice_num = 1
    while dice_num <= 5:
        print "%-5s - %5s" % ( 'Dice%s' % dice_num, dice_to_cards[dice_num] )
        dice_num += 1
        pass

    return

# Main Code
play()

sys.exit()

Here's more on using Python dictionaries.

Why bother:

import random

deck=[(x,y) for x in '23456789JQKA' for y in ['hearts','diamond','club','spade']]

random.shuffle(deck)

for i in deck: print i

Why bother:

import random

deck=[(x,y) for x in '23456789JQKA' for y in ['hearts','diamond','club','spade']]

random.shuffle(deck)

for i in deck: print i

I was under the impression that our poster, nsutton, was new to Python. Therefore, I was just trying to provide an answer without being too complex.

The only problem that I see in the code is that the number '10' is missing. Since the "A" is there, the "1" could take the place holder of "10" or the letter "T" for "Ten". I did run your code just to make sure I wasn't missing anything and this is the result:

>>> import random
>>> deck = [(x, y) for x in '23456789JQKA' for y in ['hearts', 'diamond', 'club', 'spade']]
>>> random.shuffle(deck)
>>> for i in deck:
...     print i
...
('2', 'diamond')
('3', 'hearts')
('K', 'club')
('6', 'diamond')
('2', 'spade')
('6', 'hearts')
('4', 'club')
('7', 'club')
('9', 'spade')
('9', 'hearts')
('5', 'diamond')
('8', 'spade')
('K', 'spade')
('3', 'diamond')
('Q', 'spade')
('K', 'hearts')
('6', 'spade')
('J', 'spade')
('8', 'hearts')
('4', 'diamond')
('9', 'club')
('2', 'club')
('8', 'diamond')
('Q', 'club')
('7', 'diamond')
('4', 'spade')
('5', 'spade')
('7', 'hearts')
('A', 'spade')
('K', 'diamond')
('J', 'club')
('3', 'spade')
('9', 'diamond')
('A', 'diamond')
('7', 'spade')
('Q', 'hearts')
('J', 'hearts')
('A', 'hearts')
('J', 'diamond')
('5', 'hearts')
('A', 'club')
('8', 'club')
('5', 'club')
('6', 'club')
('Q', 'diamond')
('3', 'club')
('2', 'hearts')
('4', 'hearts')
>>>

In your code, a Royal Flush is not improbable, it's impossible. :sad:

By using "T", that would give you:

>>> deck = [(x, y) for x in '23456789TJQKA' for y in ['hearts', 'diamond', 'club', 'spade']]
>>> random.shuffle(deck)
>>> for i in deck:
...     print i
...
('7', 'spade')
('A', 'spade')
('9', 'hearts')
('A', 'club')
('7', 'hearts')
('8', 'spade')
('6', 'hearts')
('3', 'club')
('T', 'spade')
('J', 'hearts')
('9', 'spade')
('2', 'spade')
('K', 'hearts')
('K', 'spade')
('6', 'diamond')
('6', 'club')
('J', 'club')
('A', 'diamond')
('K', 'club')
('5', 'club')
('4', 'spade')
('8', 'diamond')
('5', 'diamond')
('Q', 'hearts')
('J', 'spade')
('3', 'spade')
('7', 'diamond')
('T', 'diamond')
('5', 'hearts')
('3', 'hearts')
('2', 'hearts')
('T', 'club')
('9', 'diamond')
('4', 'hearts')
('6', 'spade')
('8', 'hearts')
('4', 'diamond')
('4', 'club')
('2', 'diamond')
('2', 'club')
('T', 'hearts')
('8', 'club')
('K', 'diamond')
('J', 'diamond')
('3', 'diamond')
('Q', 'diamond')
('5', 'spade')
('7', 'club')
('Q', 'club')
('9', 'club')
('Q', 'spade')
('A', 'hearts')
>>>

Or better yet....

>>> suits = ['hearts', 'diamonds', 'spade', 'clubs']
>>> cards = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
>>> deck = [(x, y) for x in cards for y in suits]
>>> random.shuffle(deck)
>>> for i in deck:
...     print i
...
('6', 'diamonds')
('6', 'hearts')
('3', 'diamonds')
('K', 'hearts')
('5', 'spade')
('9', 'spade')
('Q', 'clubs')
('10', 'hearts')
('Q', 'spade')
('3', 'spade')
('3', 'clubs')
('J', 'hearts')
('A', 'spade')
('8', 'hearts')
('4', 'spade')
('7', 'clubs')
('J', 'spade')
('9', 'clubs')
('10', 'clubs')
('Q', 'hearts')
('A', 'hearts')
('A', 'diamonds')
('5', 'clubs')
('4', 'hearts')
('K', 'diamonds')
('4', 'diamonds')
('8', 'diamonds')
('8', 'spade')
('Q', 'diamonds')
('6', 'spade')
('7', 'diamonds')
('7', 'hearts')
('2', 'spade')
('9', 'hearts')
('4', 'clubs')
('3', 'hearts')
('10', 'spade')
('2', 'hearts')
('5', 'diamonds')
('2', 'clubs')
('6', 'clubs')
('2', 'diamonds')
('K', 'clubs')
('K', 'spade')
('10', 'diamonds')
('5', 'hearts')
('J', 'diamonds')
('A', 'clubs')
('8', 'clubs')
('7', 'spade')
('J', 'clubs')
('9', 'diamonds')
>>>

Damn 2-digit number ten...always messing things up.

Sorry too quick adaptation to questions' format.

Yes that last form was what I was aiming, but forgot existence of 10.

My original program has this:

deck=[(x+1,y) for x in range(13) for y in ['hearts','diamond','club','spade']]
    random.shuffle(deck)

This is because I wanted to be easy to count together the values of cards with necessary logic for A= 1 or 14.

To pick a card you can just do

card=deck.pop()

Then you have the card only in one place, deck or hand, it is removed from deck (from end but that does not matter, or if matters do card=deck.pop(0) , which pops out the first card)

Sorry too quick adaptation to questions' format.

My original program has this:

deck=[(x+1,y) for x in range(13) for y in ['hearts','diamond','club','spade']]
    random.shuffle(deck)

To pick a card you can just do

card=deck.pop()

Then you have the card only in one place, deck or hand, it is removed from deck (from end but that does not matter, or if matters do card=deck.pop(0) , which pops out the first card)

The range() function is going to print 0 to 12, but since you're adding 1 it will give you 1 to 13. That would then just require the conversion from numbers to letters as nsutton was originally asking.

You did, however, prove that he could significantly reduce the number of lines in his code. Always a plus. :cool:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.