# Generating playing cards

This code has several functions and procedures for the program to generate cards like '5 of diamond'. I would like to shorten the code.

def random(one,two):
import random
number = random.randint(one,two)
return number

def suit():
suitnumber = random(1,4)
if suitnumber == 1:
elif suitnumber == 2:
suitb = 'Hearts'
elif suitnumber == 3:
suitb = 'Diamonds'
elif suitnumber == 4:
suitb = 'Clubs'

return suitb

def number():
number = random(1,13)
if number == 13:
value = 'Ace'
elif number == 12:
value = 'King'
elif number == 11:
value = 'Queen'
elif number == 10:
value = 'Jack'
elif number < 10:
value = number

return value

def card():
cardnumber = number()
cardsuit = suit()
card = cardsuit,cardnumber
return card

def store10Cards():
tenCards = [card(),
card(),
card(),
card(),
card(),
card(),
card(),
card(),
card(),
card()]
return tenCards

def yourCards():
cards = store10Cards()
counter = 1
choice = int(input('Which of your cards would you want to see? You have 10 cards : '))
if choice == 1:
elif choice == 2:
elif choice == 3:
elif choice == 4:
elif choice == 5:
elif choice == 6:
elif choice == 7:
elif choice == 8:
elif choice == 9:
elif choice == 10:

import time
time.sleep(5)

return cards

print('Hi')
import time
time.sleep(2)
print('You have 10 cards')
time.sleep(2)
choice = input('Would you like to see them? Y/N  : ')
choice = choice.title()
if choice == 'Y':
yourCards()
elif choice == 'N':
time.sleep(1)
import sys
sys.exit(0)

• I don't have time to write a full answer right now, but take at look at random.choice() to get started: docs.python.org/2/library/random.html#random.choice – NPE Dec 12 '14 at 19:04
• and your code for translating cards to words code be considerably shorter if tyou used lists or dictionaries. – Tony Suffolk 66 Dec 12 '14 at 19:05
• – g19fanatic Dec 12 '14 at 19:07
• This code almost looks like from a test as it contains so many portions that can be shortened. – Niklas R Dec 12 '14 at 21:53
• I'm sorry its so difficult but I'm only started lesson 5 (procedures) on Python next steps. I'm doing GCSE In computer science and I was curious if there is anyway to make this code shorter. I shall re-think what I did there and comment it. Thanks for the ideas! – karr7224 Dec 13 '14 at 9:29

You should collect all imports at the beginning of the file. You have strange card numbers and why is choice 10 special? Normally, card games have decks, and not random constructed cards, e.g. you could have 10 equal cards.

import time
import random

SUITS = ('Spade', 'Hearts', 'Diamonds', 'Clubs')
NUMBERS = (1,2,3,4,5,6,7,8,9,10,'Jack','Queen','King','Ace')

def card():
return random.choice(SUITS), random.choice(NUMBERS)

def store10Cards():
return [card() for _ in range(10)]

def yourCards():
cards = store10Cards()
choice = int(input('Which of your cards would you want to see? You have 10 cards : '))
print('Your chosen card is', cards[choice - 1])
if choice == 10:
time.sleep(5)
return cards

print('Hi')
time.sleep(2)
print('You have 10 cards')
time.sleep(2)
choice = input('Would you like to see them? Y/N  : ').upper()
if choice == 'Y':
yourCards()
elif choice == 'N':
time.sleep(1)


This wouldn't shorten your code, but it's a good idea to make sure that your code is robust (ie. handles unexpected inputs). An example from above is what if the user enters a string when prompted for a card number? Unless correct input is an assumption of this program.

I optimized it a bit:

• imports on beginning of programm if possible

• may make a function "main"

• look at the function choice of the random module

• make exceptions to handle the case of a false input

import time
import random
import sys

def random(one,two):
return random.randint(one,two)

def suit():
return random.choice( ["Spade", "Hearts", "Diamonds", "Clubs"] )

def number():
return random.choice( [i for i in range(10),'Jack','Queen','King','Ace'] )

def card():
return suit(), number()

def store10Cards():
return [card() for i in range(10)]

def yourCards():
cards = store10Cards()
choice = int(input('Which of your cards would you want to see? You have 10 cards : '))

time.sleep(5)

return cards

def main():
print("Hi")
time.sleep(2)
print('You have 10 cards')
time.sleep(2)
choice = input('Would you like to see them? Y/N  : ')
choice = choice.title()
#may handle a false input here (what if ik type something else than Y or N)
if choice == 'Y':
yourCards()
elif choice == 'N':

• range(10) is the same as i for i in range(10) but the first is preferrable. – Caridorc Dec 14 '14 at 20:26