Question
as you can see in the code below, I'm doing a simulation for a game of blackjack in python How can I get the mean
as you can see in the code below, I'm doing a simulation for a game of blackjack in python How can I get the mean card number and mode card number picked during the simulation and the average score of player 1 and player 2 during the simulation? how could I convert the results from this data into graphical format?
elif Mode == "3": Choice = input("Would you like to run a regular simulation(R) or an irregular simulation(I): ").lower() if Choice == "r": 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.choice(deck) player1Hand = [] player2Hand = [] dealerHand = [] comp_score = [] 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 = "Draws" 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, "Draws": 0}
for i in range(num_games): result = simulate_game() results[result] += 1 print(f"Player 1 won {results['Player 1']} times.") print(f"Player 2 won {results['Player 2']} times.") print(f"There were {results['Draws']} draws.") #Saving the data file = open("SimData.csv", "w", newline ="") writer = csv.writer(file) Headings = ["Games", "Won"] writer.writerow(Headings) for result, freq in results.items(): writer.writerow([result, freq]) file.close() Replay = (input("Would you like to play again(Y/N): ")).lower() if Replay == "n": print ("Thank you for playing") break elif Replay == "y": total = 0 ace_11s = 0 player1Hand = [] player2Hand = [] dealerHand = [] results = {"Player 1": 0, "Player 2": 0, "Draws": 0} Mode = input("Would you like to play single player(1), multiplayer(2) or do a simulation(3).(Type number): ") print (Mode) else: print("Please type Y/N") print(Replay)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started