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])
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':
print('Too bad')
time.sleep(1)
import sys
sys.exit(0)
3 Answers 3
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)
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:
import
s on beginning of programm if possiblemay 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 : '))
print('Your chosen card is',cards[choice-1])
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':
print('Too bad')
time.sleep(1)
if __name__ == "__main__":
main()
-
\$\begingroup\$
range(10)
is the same asi for i in range(10)
but the first is preferrable. \$\endgroup\$Caridorc– Caridorc2014年12月14日 20:26:57 +00:00Commented Dec 14, 2014 at 20:26
random.choice()
to get started: docs.python.org/2/library/random.html#random.choice \$\endgroup\$