Guest User

Blackjack simple python

a guest
Jul 20th, 2025
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.34 KB | Source Code | 0 0
  1. # BlackJack
  2.  
  3. from random import randint
  4.  
  5. # Introduction and options.
  6. print('Welcome to BlackJack')
  7. print('Please choose an option: \'Help\' or \'Play\'')
  8. response = input()
  9.  
  10.  
  11. # Main game function.
  12. def Play():
  13.     first_num = randint(1, 11)
  14.     secon_num = randint(1, 10)
  15.     player_total = first_num + secon_num
  16.  
  17.     dealer_num1 = randint(2, 11)
  18.     dealer_num2 = randint(2, 10)
  19.     dealer_total = dealer_num1 + dealer_num2
  20.  
  21.     print('Dealer\'s first number is:', dealer_num1)
  22.  
  23.     print('\nYour first number is:', first_num)
  24.     print('Your second number is:', secon_num)
  25.     print('Total:', player_total)
  26.  
  27.     print('Would you like a new number?: \'Yes/No\'')
  28.     new_number = input()
  29.     if (new_number == 'Yes' or new_number == 'yes'):
  30.         third_num = randint(1, 11)
  31.         player_total = third_num + player_total
  32.         print('Your new number is', third_num)
  33.         print('New total:', player_total)
  34.  
  35.     print('\nDealer\'s second number is:', dealer_num2)
  36.     print('Dealer\'s total:', dealer_total)
  37.  
  38.     if ((player_total > dealer_total and player_total <= 21)
  39.             or dealer_total > 21 or player_total == 21):
  40.         print('\nYou win!')
  41.     if (player_total < dealer_total or player_total > 21):
  42.         print('\nYou lose!')
  43.  
  44.  
  45. # Help function, in case the game is unclear.
  46. def Help():
  47.     print('The objective of the game is to achieve')
  48.     print('the number 21 or a higher number than')
  49.     print('the dealer. If your number is higher than')
  50.     print('21 then you are bust and you lose, however')
  51.     print('if the dealers number is above 21 and yours')
  52.     print('is below then you win.')
  53.  
  54.  
  55. # Function that loops the game so you can replay.
  56. def Play2():
  57.     Play()
  58.     start = True
  59.     while (start):
  60.         again = input('\nPlay again?: \'Yes/No\' ')
  61.         if (again == 'Yes' or again == 'yes'):
  62.             print()
  63.             Play()
  64.         if (again == 'No' or again == 'no'):
  65.             start = False
  66.  
  67.  
  68. # When options are typed, the according if statement calls a function.
  69. if (response == 'Play' or response == 'play'):
  70.     Play2()
  71.  
  72. if (response == 'Help' or response == 'help'):
  73.     Help()
  74.     # Allows the users to play after viewing the help option.
  75.     print('\nWould you like to Play now?')
  76.     play_now = input()
  77.     if (play_now == 'Yes' or play_now == 'yes'):
  78.         Play2()
Add Comment
Please, Sign In to add comment