I am making a blackjack game for class, my code works and there are no errors persay, but every time I call the play function it doesn't reset the cards you are dealt. It works if you stop and run the program again but when you say yes to try again it gives you and the dealer the same cards everytime. It isn't the same as that other question do doen't suggest it, this a different situation with a specific fix.
Here is the code:
import random
playerIn = True
dealerIn = True
deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 3, 4, 5, 6, 7, 8, 9, 10,
'J', 'Q', 'K', 'A', 'J', 'Q', 'K', 'A', 'J', 'Q', 'K', 'A', 'J', 'Q', 'K', 'A']
Typecard = ['Of Hearts', 'Of Spades', 'Of Clubs', 'Of Diamonds']
playerHand = []
dealerHand = []
def dealCard(turn):
card = random.choice(deck)
turn.append(card)
deck.remove(card)
def total(turn):
total = 0
face = ['J', 'K', 'Q' ]
for card in turn:
if card in range(1, 11):
total += card
elif card in face:
total += 10
else:
if total > 11:
total += 1
else:
total += 11
return total
def tryAgain():
again = input("Would you like to play again, type yes or no: ").lower()
if again == "yes":
print("Ok")
play()
elif again == "no":
print(f"Bye {name}")
def revealDealerHand():
if len (dealerHand) == 2:
return dealerHand[0]
elif len (dealerHand) > 2:
return dealerHand[0], dealerHand[1]
for _ in range(2):
dealCard(dealerHand)
dealCard(playerHand)
def play():
while playerIn or dealerIn:
print(f"\nDealer has {revealDealerHand()} and X")
print(f"\nYou have {playerHand} for a total of {total(playerHand)}")
if playerIn:
stayOrHit = input("\nWould you like to stay or hit (type 1 for stay and 2 for hit): ").lower()
if total(dealerHand) > 16:
dealerIn = False
else:
dealCard(dealerHand)
if stayOrHit == "1":
break
else:
dealCard(playerHand)
if total(playerHand) >= 21:
break
elif total(dealerHand) >= 21:
break
if total(playerHand) == 21:
print(f"\nYou have a hand of {playerHand} for a total of {total(playerHand)} and the dealer has {dealerHand} for a total of {total(dealerHand)}")
print(f"BLACKJAAACK! Nice one {name}")
tryAgain()
elif total(dealerHand) == 21:
print(f"\nYou have a hand of {playerHand} for a total of {total(playerHand)} and the dealer has {dealerHand} for a total of {total(dealerHand)}")
print("BLACKJACK, Dealer wins you lose, HA!")
tryAgain()
elif total(playerHand) > 21:
print(f"\nYou have a hand of {playerHand} for a total of {total(playerHand)} and the dealer has {dealerHand} for a total of {total(dealerHand)}")
print("You bust loser, Dealer wins.")
tryAgain()
elif total(dealerHand) > 21:
print(f"\nYou have a hand of {playerHand} for a total of {total(playerHand)} and the dealer has {dealerHand} for a total of {total(dealerHand)}")
print(f"The dealer busts, You win {name}!")
tryAgain()
elif 21 - total(dealerHand) < 21 - total(playerHand):
print(f"\nYou have a hand of {playerHand} for a total of {total(playerHand)} and the dealer has {dealerHand} for a total of {total(dealerHand)}")
print("Dealer Wins, loser.")
tryAgain()
elif 21 - total(playerHand) < 21 - total(dealerHand):
print(f"\nYou have a hand of {playerHand} for a total of {total(playerHand)} and the dealer has {dealerHand} for a total of {total(dealerHand)}")
print(f"You Win {name}!")
tryAgain()
name = input("Please type you name: ")
play()
I hvaen't found anything on this problem so nothing really.
Aucun commentaire:
Enregistrer un commentaire