0
\$\begingroup\$

At first I have three cards like this:

 ___    ___    ___
|   |  |   |  |   | 
| A |  | A |  | Q |
|___|  |___|  |___|

and then I shuffle them to this:

 ___    ___    ___
|   |  |   |  |   |
| 1 |  | 2 |  | 3 |
|___|  |___|  |___|

So now I want to write a function that generates a random number between 1 and 3 and assign it to be the value of the Q card. For example if I generate number 2, the cards when turned around will have this order:

 ___    ___    ___
|   |  |   |  |   |
| A |  | Q |  | A |
|___|  |___|  |___|

So I came up with a code like this

def CreateCard(card):
    print (' _____')
    print ('|     |')
    print ('|  ',card,'  |',sep='')
    print ('|_____|')

def shuffle():
    Q = random.randint(1,4)
    if Q == 1:
        cards = ['Q','A','A']
    elif Q == 2:
        cards = ['A','Q','A']
    else:
        cards = ['A','A','Q']
    return cards
for card in shuffle():
   CreateCard(card)

I guess what I'm asking is this the optimal way since I don't think I'm allowed to use any other function of random module besides randint or randrange.

\$\endgroup\$
1
  • 3
    \$\begingroup\$ I find your description of the problem mildly confusing. Are you just looking to generate a random permutation of the three cards? If you are, take a look at docs.python.org/2/library/random.html#random.shuffle \$\endgroup\$
    – NPE
    Commented Feb 19, 2015 at 7:59

2 Answers 2

1
\$\begingroup\$

Style

The name CreateCard is pretty bad. One would expect it to create a card and return it.

Making things easier to change

At the moment, if you ever want to use 4 or 5 cards instead of 3, you have to update code in multiple places : Q = random.randint(1,HERE), cards = HERE (3 times) and add tests like elif Q == VALUE:. This is just too easy to get wrong.

Even assuming you can only use randint, you have various ways to achieve what you are doing in a less repetitive way. First idea would be to tell that once you've generated the position for Q with q_pos = random.randint(1,nb_cards), you know you have q_pos -1 'A' cards before it and nb_cards - q_pos after.

This becomes :

def shuffle(nb_cards):
    q_pos = randint(1,nb_cards)
    return ['A'] *  (q_pos -1) + ['Q'] + ['A'] * (nb_cards - q_pos)

A different strategy would be to create a list of 'A's and just put Q somewhere.

def shuffle(nb_cards):
    cards = ['A'] * nb_cards
    q_pos = randint(0,nb_cards-1)
    cards[q_pos] = 'Q'
    return cards
\$\endgroup\$
0
\$\begingroup\$
import random

def ShowCards(cards):
    print (' ___    ___    ___')
    print ('|   |  |   |  |   |')
    print ('| %s |  | %s |  | %s |' % (cards[0], cards[1], cards[2]))
    print ('|___|  |___|  |___|')

cards = ['A', 'A', 'Q']  # Generate list with cards
random.shuffle(cards)  # Randomly reorder the cards    
ShowCards(cards)    # Print cards random order

Output:

 ___    ___    ___
|   |  |   |  |   |
| A |  | A |  | Q |
|___|  |___|  |___|
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Moderator's note: This answer was posted before the question was migrated to Code Review. It does not meet Code Review standards for answers, as it offers an alternate solution without reviewing the code in the question. \$\endgroup\$ Commented Feb 20, 2015 at 2:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.