Question: im trying to do a python blackjack simulation where the user puts in an input for the amount of times they want to run the

im trying to do a python blackjack simulation where the user puts in an input for the amount of times they want to run the simulation but everytime the code is run, the cards from each of the simulation are identical and this defeats the purpose of having randomness and more so, i dont get the results back to see how many wins player 1 and player 2 had, aswell as the amount of draws recorded how can all these issues be solved? here is the code:

elif Mode == "3": print("we will do a simulation") def dealCard(hand, deck): card = random.choice(deck) hand.append(card) deck.remove(card) def total(hand): total = 0 ace_11s = 0 for card in hand: if card in range(2, 11): total += card elif card in ["J", "K", "Q"]: total += 10 else: total += 11 ace_11s += 1 while ace_11s and total > 21: total -= 10 ace_11s -= 1 return total def simulate_game(): 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"] random.shuffle(deck) player1Hand = [] player2Hand = [] dealerHand = [] dealCard(player1Hand, deck) dealCard(player2Hand, deck) dealCard(dealerHand, deck) dealCard(player1Hand, deck) dealCard(player2Hand, deck) dealCard(dealerHand, deck) player1St = True player2St = True dealerSt = True while player1St or player2St or dealerSt: print(f"Dealer has {dealerHand[0]} and X") print(f"Player 1 has {player1Hand} for a total of {total(player1Hand)} ") print(f"Player 2 has {player2Hand} for a total of {total(player2Hand)} ") if player1St: if total(player1Hand) > 21: player1St = False else: if total(dealerHand) > 16: dealerSt = False else: dealCard(dealerHand, deck) if total(player1Hand) >= 17: player1St = False else: dealCard(player1Hand, deck) if player2St: if total(player2Hand) > 21: player2St = False else: if total(dealerHand) > 16: dealerSt = False else: dealCard(dealerHand, deck) if total(player2Hand) >= 17: player2St = False else: dealCard(player2Hand, deck) if total(dealerHand) > 21: dealerSt = False elif total(dealerHand) >= 17: dealerSt = False if total(player1Hand) > 21: result = "Player 2" elif total(player2Hand) > 21: result = "Player 1" elif total(player1Hand) == total(player2Hand): result = "Draw" elif total(player1Hand) > total(player2Hand): result = "Player 1" else: result = "Player 2" return result num_games = int(input("How many times will we run this simulation?: ")) results = {"Player 1": 0, "Player 2": 0, "Draw": 0}

for i in range(num_games): result = simulate_game() results[result] += 1

print(f"Player 1 won {results['Player1']} times.") print(f"Player 2 won {results['Player2']} times.") print(f"There were {results['tie']} ties.")

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!