Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. import random
  2.  
  3. name = input("Please enter your name: ")
  4. player_score = 0
  5. computer_score = 0
  6. player_count = 2
  7. computer_count = 2
  8. keepPlaying = True
  9. deal = 'y'
  10.  
  11. def getCard(score) :
  12. card = random.randint(1,11)
  13. score = score + card
  14. return score
  15.  
  16. def checkResults(player_score, computer_score) :
  17. if player_score < 21 and computer_score < 21 :
  18. return True
  19. elif player_score > 21 and computer_score < 21 :
  20. print("You bust! The dealer wins.")
  21. return False
  22. elif player_score < 21 and computer_score > 21 :
  23. print("The dealer busts. You win!")
  24. return False
  25. elif player_score == 21 and computer_score < 21 :
  26. print("Blackjack! You win!")
  27. return False
  28. elif computer_score == 21 and player_score < 21 :
  29. print("Blackjack! The dealer wins.")
  30. return False
  31.  
  32. def displayScore(player_score, computer_score) :
  33. print("")
  34. print("~ Score ~")
  35. print("{0}: {1}".format(name, player_score))
  36. print("Dealer: ", computer_score)
  37.  
  38. player_score = getCard(player_score)
  39. computer_score = getCard(computer_score)
  40. player_score = getCard(player_score)
  41. computer_score = getCard(computer_score)
  42. displayScore(player_score, computer_score)
  43. keepPlaying = checkResults(player_score, computer_score)
  44.  
  45.  
  46. while keepPlaying == True and deal == 'y' :
  47. print("Would you like another card?")
  48. deal = input("Enter 'y' or 'n': ")
  49. deal = deal.lower()
  50. while deal != 'y' and deal != 'n':
  51. print("Error: Invalid input.")
  52. deal = input("Enter 'y' or 'n': ")
  53.  
  54. if deal == 'y':
  55. player_score = getCard(player_score)
  56. player_count = player_count + 1
  57. displayScore(player_score, computer_score)
  58. keepPlaying = checkResults(player_score, computer_score)
  59. elif deal == 'n':
  60. while computer_score < 16 or computer_score < player_score :
  61. computer_score = getCard(computer_score)
  62. computer_count = computer_count + 1
  63. displayScore(player_score, computer_score)
  64. keepPlaying = checkResults(player_score, computer_score)
  65.  
  66. if player_score < 21 and computer_score < 21 :
  67. if player_score > computer_score :
  68. print("You win!")
  69. elif player_score == computer_score :
  70. if player_count > computer_count :
  71. print("Tied score - the dealer wins with less cards.")
  72. elif computer_count < player_count :
  73. print("Tied score - you win with less cards!")
  74. else :
  75. print("Tie!")
  76. else :
  77. print("The dealer wins.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement