[Tutor] deck dealing program

python-tutor@toddmaynard.com python-tutor at toddmaynard.com
Mon Aug 8 09:02:09 CEST 2005


Are you:
  a.) Having trouble with the code and looking for help?
  b.) Looking for suggestions on how to improve the code?
  c.) Offering the code as a demo for Nathan et al.?

I was just doing stuff along the same lines and was having fun seeing the 
different approaches to the same problem.  

--Todd 

On Monday 08 August 2005 02:38 am, luke wrote:
> from random import randint
>
> def identify_card(n):
>     cardname = ""
>     royals = ["Jack","Queen","King","Ace"]
>     temp = n % 13
>     if temp > 8:
>         cardname += royals[temp-9]
>     else:
>         cardname += str(temp+2)
>     cardname += " of "
>
>     suits = ["Spades","Hearts","Diamonds","Clubs"]
>     cardname += suits[n/13]
>     return cardname
>
> def main():
>     deck = range(52)
>     cards = []
>     while 1:
>         x = raw_input("how many cards do you want? ")
>         try:
>             x = int(x)
>         except ValueError:
>             print "Invalid value exiting for I have no error code. Please
> use an int next time." raise SystemExit
>         if x <= 52 and x >= 0:
>             y = 0
>             while y < x:
>                
> cards.append(identify_card(deck.pop(randint(0,len(deck)-1)))) y += 1
>             break
>     print cards
> if __name__ == "__main__":
>     main()
>
> #Luke


More information about the Tutor mailing list