patternpythonMinor
Generating playing cards
Viewed 0 times
playingcardsgenerating
Problem
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:
suitb = 'Spade'
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:
print('Your chosen card is',cards[0])
elif choice == 2:
print('Your chosen card is',cards[1])
elif choice == 3:
print('Your chosen card is',cards[2])
elif choice == 4:
print('Your chosen card is',cards[3])
elif choice == 5:
print('Your chosen card is',cards[4])
elif choice == 6:
print('Your chosen card is',cards[5])
elif choice == 7:
print('Your chosen card is',cards[6])
elif choice == 8:
print('Your chosen card is',cards[7])
elif choice == 9:
print('Your chosen card is',cards[8])
elif choice == 10:
print('Your chosen card is',cards[9])
```
def random(one,two):
import random
number = random.randint(one,two)
return number
def suit():
suitnumber = random(1,4)
if suitnumber == 1:
suitb = 'Spade'
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:
print('Your chosen card is',cards[0])
elif choice == 2:
print('Your chosen card is',cards[1])
elif choice == 3:
print('Your chosen card is',cards[2])
elif choice == 4:
print('Your chosen card is',cards[3])
elif choice == 5:
print('Your chosen card is',cards[4])
elif choice == 6:
print('Your chosen card is',cards[5])
elif choice == 7:
print('Your chosen card is',cards[6])
elif choice == 8:
print('Your chosen card is',cards[7])
elif choice == 9:
print('Your chosen card is',cards[8])
elif choice == 10:
print('Your chosen card is',cards[9])
Solution
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':
print('Too bad')
time.sleep(1)Code Snippets
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':
print('Too bad')
time.sleep(1)Context
StackExchange Code Review Q#73614, answer score: 5
Revisions (0)
No revisions yet.