Github Blackjack Python
Posted onby admin
Github Blackjack Python Average ratng: 9,9/10 7726 votes
(I hope its okay that I link the GitHub BlackJack game click here as it would be a few hundred lines to copy and paste here. The player has ability to wager on each hand, and keep playing until balance hits $0.0. As a future project, I'm thinking of maybe having a way to have user provide a login, to remember the balance/ and they can pick off. Python blackjack. GitHub Gist: instantly share code, notes, and snippets.
Forked from mjhea0/python_blackjack.py
python_blackjack.py
Github Python Projects
importos |
importrandom |
deck= [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]*4 |
defdeal(deck): |
hand= [] |
foriinrange(2): |
random.shuffle(deck) |
card=deck.pop() |
ifcard11:card='J' |
ifcard12:card='Q' |
ifcard13:card='K' |
ifcard14:card='A' |
hand.append(card) |
returnhand |
defplay_again(): |
again=raw_input('Do you want to play again? (Y/N) : ').lower() |
ifagain'y': |
dealer_hand= [] |
player_hand= [] |
deck= [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]*4 |
game() |
else: |
print'Bye!' |
exit() |
deftotal(hand): |
total=0 |
forcardinhand: |
ifcard'J'orcard'Q'orcard'K': |
total+=10 |
elifcard'A': |
iftotal>=11: total+=1 |
else: total+=11 |
else: |
total+=card |
returntotal |
defhit(hand): |
card=deck.pop() |
ifcard11:card='J' |
ifcard12:card='Q' |
ifcard13:card='K' |
ifcard14:card='A' |
hand.append(card) |
returnhand |
defclear(): |
ifos.name'nt': |
os.system('CLS') |
ifos.name'posix': |
os.system('clear') |
defprint_results(dealer_hand, player_hand): |
clear() |
print'The dealer has a '+str(dealer_hand) +' for a total of '+str(total(dealer_hand)) |
print'You have a '+str(player_hand) +' for a total of '+str(total(player_hand)) |
defblackjack(dealer_hand, player_hand): |
iftotal(player_hand) 21: |
print_results(dealer_hand, player_hand) |
print'Congratulations! You got a Blackjack!n' |
play_again() |
eliftotal(dealer_hand) 21: |
print_results(dealer_hand, player_hand) |
print'Sorry, you lose. The dealer got a blackjack.n' |
play_again() |
defscore(dealer_hand, player_hand): |
iftotal(player_hand) 21: |
print_results(dealer_hand, player_hand) |
print'Congratulations! You got a Blackjack!n' |
eliftotal(dealer_hand) 21: |
print_results(dealer_hand, player_hand) |
print'Sorry, you lose. The dealer got a blackjack.n' |
eliftotal(player_hand) >21: |
print_results(dealer_hand, player_hand) |
print'Sorry. You busted. You lose.n' |
eliftotal(dealer_hand) >21: |
print_results(dealer_hand, player_hand) |
print'Dealer busts. You win!n' |
eliftotal(player_hand) <total(dealer_hand): |
print_results(dealer_hand, player_hand) |
print'Sorry. Your score isn't higher than the dealer. You lose.n' |
eliftotal(player_hand) >total(dealer_hand): |
print_results(dealer_hand, player_hand) |
print'Congratulations. Your score is higher than the dealer. You winn' |
defgame(): |
choice=0 |
clear() |
print'WELCOME TO BLACKJACK!n' |
dealer_hand=deal(deck) |
player_hand=deal(deck) |
whilechoice!='q': |
print'The dealer is showing a '+str(dealer_hand[0]) |
print'You have a '+str(player_hand) +' for a total of '+str(total(player_hand)) |
blackjack(dealer_hand, player_hand) |
choice=raw_input('Do you want to [H]it, [S]tand, or [Q]uit: ').lower() |
clear() |
ifchoice'h': |
hit(player_hand) |
whiletotal(dealer_hand) <17: |
hit(dealer_hand) |
score(dealer_hand, player_hand) |
play_again() |
elifchoice's': |
whiletotal(dealer_hand) <17: |
hit(dealer_hand) |
score(dealer_hand, player_hand) |
play_again() |
elifchoice'q': |
print'Bye!' |
exit() |
if__name__'__main__': |
game() |
Github Python Tutorial
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment