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)
\$\endgroup\$

migrated from stackoverflow.com Dec 14 '14 at 16:41

This question came from our site for professional and enthusiast programmers.

  • 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\$ – NPE Dec 12 '14 at 19:04
  • \$\begingroup\$ and your code for translating cards to words code be considerably shorter if tyou used lists or dictionaries. \$\endgroup\$ – Tony Suffolk 66 Dec 12 '14 at 19:05
  • \$\begingroup\$ This code almost looks like from a test as it contains so many portions that can be shortened. \$\endgroup\$ – Niklas R Dec 12 '14 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\$ – karr7224 Dec 13 '14 at 9:29
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)
\$\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.

\$\endgroup\$
0
\$\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()
\$\endgroup\$
  • \$\begingroup\$ range(10) is the same as i for i in range(10) but the first is preferrable. \$\endgroup\$ – Caridorc Dec 14 '14 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy