4
\$\begingroup\$

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)
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Dec 12, 2014 at 19:01
\$\endgroup\$
5
  • 1
    \$\begingroup\$ 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 \$\endgroup\$ Commented Dec 12, 2014 at 19:04
  • \$\begingroup\$ and your code for translating cards to words code be considerably shorter if tyou used lists or dictionaries. \$\endgroup\$ Commented Dec 12, 2014 at 19:05
  • \$\begingroup\$ also look at docs.python.org/2/tutorial/introduction.html#lists and docs.python.org/2/tutorial/controlflow.html#for-statements \$\endgroup\$ Commented Dec 12, 2014 at 19:07
  • \$\begingroup\$ This code almost looks like from a test as it contains so many portions that can be shortened. \$\endgroup\$ Commented Dec 12, 2014 at 21:53
  • \$\begingroup\$ 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! \$\endgroup\$ Commented Dec 13, 2014 at 9:29

3 Answers 3

5
\$\begingroup\$

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)
answered Dec 12, 2014 at 19:15
\$\endgroup\$
3
\$\begingroup\$

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.

answered Mar 31, 2019 at 7:56
\$\endgroup\$
1
\$\begingroup\$

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 : '))
 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()
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
answered Dec 12, 2014 at 19:23
\$\endgroup\$
1
  • \$\begingroup\$ range(10) is the same as i for i in range(10) but the first is preferrable. \$\endgroup\$ Commented Dec 14, 2014 at 20:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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