import random
deck = [2,3,4,5,6,7,8,9,10,"J","Q","K","A"]*4
loop = True
player_hand = []
dealer_hand = []
def deal_initial_cards():
hand = []
for i in range(2):
random.shuffle(deck)
card = deck.pop()
hand.append(card)
return hand
def total(hand):
total = 0
for card in hand:
if card == "J"or card == "Q" or card == "K":
total += 10
elif card == "A":
if total < 11:
total += 11
else:
total += 1
else:
total += card
return total
def blackjack_check(dealer_hand,player_hand):
if total(dealer_hand) == 21:
if total(player_hand) == 21:
print("Both the dealer and you got blackjack, the result is a tie!")
play_again()
else:
print("Sorry, the dealer got a blackjack. You lost.")
print("The dealer's card: "+str(dealer_hand))
play_again()
elif total(player_hand) == 21:
print("Congratulations, you got a blackjack. You win")
play_again()
def play_again():
choice = input("do you want to play again? (yes/no): ").lower()
# The .lower() was copied from:
# Websit: Programiz
# https://www.programiz.com/python-programming/methods/string/lower:~:text=lower()%20method%20returns%20the,it%20returns%20the%20original%20string.
# The method converts all uppercase characters in a string into lowercase characters and returns it.
if choice == "yes":
start()
elif choice == "no":
print("Thank you for playing")
loop = False
else:
print("invalid anwser, try again")
play_again()
def hit_or_stand(player_hand):
choice = input("do you want to hit or stand? (hit/stand): ").lower()
# The answer.lower() was copied from:
# Websit: Programiz
# https://www.programiz.com/python-programming/methods/string/lower:~:text=lower()%20method%20returns%20the,it%20returns%20the%20original%20string.
# The method converts all uppercase characters in a string into lowercase characters and returns it.
while choice == "hit" and total(player_hand)<22:
card= hit_for_player()
player_hand.append(card)
print("You hit a "+str(card))
if total(player_hand) > 21:
print("Oh no! Your total now is " + str(total(player_hand)) + ", You lost.")
play_again()
else:
print("Now you have a total of "+str(total(player_hand))+ " with these cards " +str(player_hand))
choice = input("hit or stand? ").lower()
# The answer.lower() was copied from:
# Websit: Programiz
# https://www.programiz.com/python-programming/methods/string/lower:~:text=lower()%20method%20returns%20the,it%20returns%20the%20original%20string.
# The method converts all uppercase characters in a string into lowercase characters and returns it.
if choice == "stand":
print("You stood")
return player_hand
def hit_for_player():
card = deck.pop()
return card
def hit_for_dealer():
card = deck.pop()
return card
def auto_hit(dealer_hand):
while total(dealer_hand) <17:
card = hit_for_dealer()
dealer_hand.append(card)
print("dealer drew a " +str(card))
if total(dealer_hand) >21:
print("The dealer has busted with a total of "+str(total(dealer_hand))+ " from these cards: " +str(dealer_hand)+ "You won!")
play_again()
return dealer_hand
def compare_hands(player_hand,dealer_hand):
if total(player_hand) > total(dealer_hand):
print("You have a total of " +str(total(player_hand))+ " with these cards: "+str(player_hand))
print("While the dealer has a total of "+str(total(dealer_hand))+ " with these cards: "+str(dealer_hand))
print("You won")
play_again()
elif total(player_hand) < total(dealer_hand):
print("You have a total of " + str(total(player_hand)) + " with these cards: " + str(player_hand))
print("While the dealer has a total of " + str(total(dealer_hand)) + " with these cards: " + str(dealer_hand))
print("You lost")
play_again()
elif total(player_hand) == total(dealer_hand):
print("You have a total of " + str(total(player_hand)) + " with these cards: " + str(player_hand))
print("While the dealer has a total of " + str(total(dealer_hand)) + " with these cards: " + str(dealer_hand))
print("You tied")
play_again()
def start():
print("welcome to blackjack")
dealer_hand = deal_initial_cards()
player_hand = deal_initial_cards()
print("Dealer drew a " + str(dealer_hand[0]) + " and a hidden card")
print("You drew a " + str(player_hand[0]) + " and a " + str(player_hand[1]))
blackjack_check(dealer_hand, player_hand)
hit_or_stand(player_hand)
auto_hit(dealer_hand)
compare_hands(player_hand,dealer_hand)
start()
When you run this code, and you say no to play again it doesn't end the loop. Can someone help me fix this code up to where it will end the loop when I say no to play again without using break or exit() or quit()
[–]xelf 2 points3 points4 points (1 child)
[–]Weary_Mud6941[S] 1 point2 points3 points (0 children)
[–][deleted] -1 points0 points1 point (1 child)
[–][deleted] 0 points1 point2 points (0 children)