I am new to programming, and I am doing some homework to get more hands on coding experience. I have written a blackjack game in Python 3 and would like a code review of any and all of my code.
# Simple program simulates Blackjack game.
# Using method: Top-Down design, spiral development
from random import randrange
def main():
printIntro()
player_hand = player()
dealer_hand = dealer()
player_win, dealer_win = compare_between(player_hand, dealer_hand)
printResult(player_hand, dealer_hand, player_win, dealer_win)
def printIntro():
print("Blackjack (twenty-one) is a casino game played with cards.")
print("the goal of game is to draw cards that total as close to 21 points, as possibale")
print("without going over( whose hand > 21 will bust). All face cards count as 10 points,")
print("aces count as 1 or 11, and all other cards count their numeric value.")
print("\nFirstly, your turn:")
def player():
hand = []
ans = "hit"
hand.append(card())
# Ask user whether Hit or Stand?
# Condition True, if user want to Hit.
while ans[0] == "h" or ans[0] == "H":
hand.append(card())
hand = eval_ace(hand)
print("Your hand: {0} total = {1}".format(hand, sum(hand)))
if bust(hand):
break
if blackjack(hand):
break
ans = input("Do you want to Hit or Stand (H or S)? ")
return hand
def card():
# get arbitrary card from 2 to 11.
shuffle_card = randrange(2, 11 + 1)
return shuffle_card
def eval_ace(hand):
# Determine Ace = 1 or 11, relying on total hand.
total = sum(hand)
for ace in hand:
if ace == 11 and total > 21:
# at position, where Ace == 11, replace by Ace == 1.
position_ace = hand.index(11)
hand[position_ace] = 1
return hand
def bust(hand):
# Condition True: if the hand of player (or dealer) > 21.
total = sum(hand)
if total > 21:
return True
def blackjack(hand):
# Condition True: if the hand of player (or dealer) == 21.
total = sum(hand)
if total == 21:
return True
def dealer():
hand = []
hand.append(card())
while sum(hand) < 18:
hand.append(card())
hand = eval_ace(hand)
return hand
def compare_between(player, dealer):
total_player = sum(player)
total_dealer = sum(dealer)
player_bust = bust(player)
dealer_bust = bust(dealer)
player_blackjack = blackjack(player)
dearler_blackjack = blackjack(dealer)
player_win = 0
dealer_win = 0
# when player (dealer) bust.
if player_bust:
if not dearler_blackjack and total_dealer < 21:
dealer_win += 1
if dealer_bust:
if not player_blackjack and total_player < 21:
player_win += 1
if player_bust and dealer_bust:
if total_player > total_dealer:
player_win += 1
elif total_dealer > total_player:
dealer_win += 1
else:
player_win == dealer_win
# when player (dealer) get blackjack.
if player_blackjack:
player_win += 1
if dearler_blackjack:
dealer_win += 1
if player_blackjack and dearler_blackjack:
player_win == dealer_win
# when total hand of player (dealer) < 21.
if total_player < 21 and total_dealer < 21:
if total_player > total_dealer:
player_win += 1
elif total_dealer > total_player:
dealer_win += 1
else:
player_win == dealer_win
return player_win, dealer_win
def printResult(player_hand, dealer_hand, player_win, dealer_win):
print("\nWe have the result: ")
print("Player has: {0} total = {1}".format(player_hand, sum(player_hand)))
print("Dealer has: {0} total = {1}".format(dealer_hand, sum(dealer_hand)))
print("player: {} | dealer: {}".format(player_win, dealer_win))
if __name__ == "__main__": main()
Update
After a few hour to learn and apply PEP8. Here is my version 2:
# Program simulate Blackjack game.
# with multiple game
# Using method: Top-Down design, spiral development
from random import randrange
def main():
print_intro()
player_win, dealer_win, game = play_multiple_game()
final_result(player_win, dealer_win, game)
def print_intro():
print("Blackjack (twenty-one) is a casino game played with cards.")
print("the goal of game is to draw cards that total as close to 21 points, as possibale")
print("without going over (whose hand > 21 will is_bust). All face cards count as 10 points,")
print("aces count as 1 or 11, and all other cards count their numeric value.")
print("Firstly, your turn:")
return None
def play_multiple_game():
player_win = 0
dealer_win = 0
game = 0
play_again = "yes"
# Ask user whether continue another game or stop
# Condition True, if user want to play.
while (play_again[0] == "y" or play_again[0] == "Y"):
player_hand = player_turn()
dealer_hand = dealer_turn()
player_score, dealer_score = compare_between(player_hand, dealer_hand)
result_of_this_game(player_hand, dealer_hand)
if (player_score > dealer_score):
print("\nPlayer win!")
player_win += 1
elif (dealer_score > player_score):
print("\nDealer win!")
dealer_win += 1
else:
print("\nThis game end in a tie!")
player_win == dealer_win
game += 1
play_again = input("\nDo you want to continue (Y or N)? ")
return player_win, dealer_win, game
def player_turn():
hand = []
ans = "hit"
hand.append(take_card())
# Ask user whether Hit or Stand?
# Condition True, if user want to Hit.
while (ans[0] == "h" or ans[0] == "H"):
hand.append(take_card())
hand = eval_ace(hand)
print("\nYour hand: {0} total = {1}".format(hand, sum(hand)))
if (is_bust(hand) or
is_blackjack(hand)):
break
ans = input("Do you want to Hit or Stand (H or S)?")
return hand
def take_card():
# get arbitrary card from 2 to 11.
shuffle_card = randrange(2, 11 + 1)
return shuffle_card
def eval_ace(hand):
# Determine Ace = 1 or 11, relying on total hand.
total = sum(hand)
for ace in hand:
if (ace == 11 and total > 21):
# at position, where Ace == 11, replace by Ace == 1.
position_ace = hand.index(11)
hand[position_ace] = 1
return hand
def is_bust(hand):
# Condition True: if the hand of player (or dealer) > 21.
total = sum(hand)
if total > 21:
return True
return None
def is_blackjack(hand):
# Condition True: if the hand of player (or dealer) == 21.
total = sum(hand)
if total == 21:
return True
return None
def dealer_turn():
hand = []
while sum(hand) < 18:
hand.append(take_card())
hand = eval_ace(hand)
return hand
def compare_between(player, dealer):
total_player = sum(player)
total_dealer = sum(dealer)
player_bust = is_bust(player)
dealer_bust = is_bust(dealer)
player_blackjack = is_blackjack(player)
dearler_blackjack = is_blackjack(dealer)
player_score = 0
dealer_score = 0
# when player (dealer) is_bust.
if player_bust:
if (not dearler_blackjack and
total_dealer < 21):
dealer_score += 1
if dealer_bust:
if (not player_blackjack and
total_player < 21):
player_score += 1
if (player_bust and
dealer_bust):
if (total_player > total_dealer):
player_score += 1
elif (total_dealer > total_player):
dealer_score += 1
else:
player_score == dealer_score
# when player (dealer) get blackjack.
if player_blackjack:
player_score += 1
if dearler_blackjack:
dealer_score += 1
if (player_blackjack and
dearler_blackjack):
player_score == dealer_score
# when total hand of player (dealer) < 21.
if (total_player < 21 and
total_dealer < 21):
if (total_player > total_dealer):
player_score += 1
elif (total_dealer > total_player):
dealer_score += 1
else:
player_score == dealer_score
return player_score, dealer_score
def result_of_this_game(player_hand, dealer_hand):
print("\nWe have the result: ")
print("Player has: {0} total = {1}".format(
player_hand, sum(player_hand)))
print("Dealer has: {0} total = {1}".format(
dealer_hand, sum(dealer_hand)))
return None
def final_result(player_win, dealer_win, game):
print("\nThe Final after {} games:".format(game))
print("player: {} | dealer: {}".format(
player_win, dealer_win))
return None
if __name__ == "__main__": main()