Hello,
I am currently studying using codecademy to learn more about coding with the hopes that in the future I will be able to change my career over to software development. I made this game as part of my course, if anyone could offer any pointers on where I could have done better, please feel free to let me know! I know the logic isn't 100% accurate, as it doesn't include aces, however I don't think I'm at the level I need to be at to implement that logic.
Thank you for reading :)
import random
def draw_card():
return random.randint(1,10)
def start_game():
dealer_hand = [draw_card()]
player_hand = [draw_card(), draw_card()]
print("Welcome to blackjack!")
print("Dealer has {d}\nPlayer has {p}".format(d = sum(dealer_hand), p = sum(player_hand)))
#player turn
while True:
act = input("Hit or stand on {p}? ".format(p = sum(player_hand))).lower()
if act == "hit":
player_hand.append(draw_card())
print("Player draws a {new}. Player hand: {p}".format(new = player_hand[-1], p = sum(player_hand)))
total = sum(player_hand)
if total > 21:
print("Player busts on {p}! Dealer wins!".format(p = sum(player_hand)))
return
elif total == 21:
print("21!")
break
elif act == "stand":
print("Player stands on {p}".format(p = sum(player_hand)))
break
else:
act = input("Please type \"Hit\" or \"Stand\"")
#dealer turn
print("Dealer has {d}".format(d = sum(dealer_hand)))
while sum(dealer_hand) < 17:
dealer_hand.append(draw_card())
print("Dealer draws a {new}\nDealer has {d}".format(new = dealer_hand[-1], d = sum(dealer_hand)))
print("Player has {p}, dealer has {d}".format(p = sum(player_hand), d = sum(dealer_hand)))
if sum(dealer_hand) > 21:
print("Dealer busts! You win!")
elif sum(dealer_hand) == sum(player_hand):
print("Its a tie!")
elif sum(dealer_hand) > sum(player_hand):
print("Dealer wins.")
elif sum(player_hand) > sum(dealer_hand):
print("You win!")
game_over = False
while not game_over:
start_game()
answer = input("Play again?(y/n) ")
game_over = answer[0] == "n"
Top comments (0)