Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: Python  |  size: 1.29 KB  |  hits: 29  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #Black jack game
  2. #python 2.7
  3.  
  4. import random
  5.  
  6. cards = []
  7.  
  8.  
  9. def game():
  10.     print "You have " + str(len(cards)) + " card(s)"
  11.     print "Current cards:"
  12.     print cards
  13.     print "1 - Hit\n2 - Stand"
  14.     usrInput = int(raw_input("?"))
  15.     if usrInput == 1:
  16.         cards.append(random.randrange(1,12))
  17.         print cards
  18.         if sum(cards) == 21:
  19.             print "You win!"
  20.         elif sum(cards) > 21:
  21.             print "You lose :("
  22.         elif sum(cards) < 21:
  23.             game()
  24.     else:
  25.         dealerSum = random.randrange(17, 22)
  26.         print "The dealer had: " + str(dealerSum)
  27.         print "You had: " + str(sum(cards))
  28.         if sum(cards) > dealerSum and not sum(cards) == 21:
  29.             print "You win!"
  30.         elif sum(cards) == dealerSum:
  31.             print "You tie!"
  32.         elif sum(cards) < dealerSum:
  33.             print "You lose :("
  34. def main():
  35.    
  36.     isQuit = False
  37.     while isQuit == False:
  38.         print "=================="
  39.         print "1 - Play\n2 - Quit"
  40.         try:
  41.             usrInput = int(raw_input("?"))
  42.             if usrInput == 2:
  43.                 isQuit = True
  44.             elif usrInput == 1:
  45.                 del cards[:]
  46.                 game()
  47.         except ValueError:
  48.             print "Invalid Input"
  49.  
  50.  
  51. main()