make it so the python code becomes a dynamic bar chart using from matplotlib import animation. Make it so the bar moves. This is the code I have so far for the python code import random import sys import matplotlib.pyplot as plt import pandas as pd
make it so the python code becomes a dynamic bar chart using from matplotlib import animation. Make it so the bar moves.
This is the code I have so far for the python code
import random
import sys
import matplotlib.pyplot as plt
import pandas as pd
def roll_dice():
die1 = random.randrange(1, 7)
die2 = random.randrange(1, 7)
return (die1, die2)
def display_dice(dice):
die1, die2 = dice
print(f'Player rolled {die1} + {die2} = {sum(dice)}')
# List that stores number of wins on every roll
winList = []
# List that stores number of losses on every roll
lossList = []
# List that stores label indexes of horizontal bar plot
ylabel = []
# 1
# number of games of craps
n = int(input("Enter number of games: "))
# Iterating 13 times
# Because it is mentioned in the question that plot should have 13
# horizontal bars for wins, and 13 horizontal bars for losses.
for roll in range(13):
ylabel.append('Roll ' + str(roll + 1))
# variables that keep track of wins and losses on every roll
# for the given number of games of craps
wins = losses = 0
for i in range(n):
die_values = roll_dice()
display_dice(die_values)
sum_of_dice = sum(die_values)
if sum_of_dice in (7, 11):
game_status = 'WON'
wins += 1
elif sum_of_dice in (2, 3, 12):
game_status = 'LOST'
losses += 1
else:
game_status = 'CONTINUE'
my_point = sum_of_dice
print('Point is', my_point)
while game_status == 'CONTINUE':
die_values = roll_dice()
display_dice(die_values)
sum_of_dice = sum(die_values)
if sum_of_dice == my_point:
game_status = 'WON'
wins += 1
elif sum_of_dice == 7:
game_status = 'LOST'
losses += 1
if game_status == 'WON':
print('Player wins')
else:
print('Player loses')
winList.append(wins)
lossList.append(losses)
# 2
data = {'Wins': winList, 'Losses': lossList}
df = pd.DataFrame(data, columns=['Wins', 'Losses'], index=ylabel)
df.plot.barh()
plt.title('Game of Craps')
plt.ylabel('Roll')
plt.xlabel('Number of Games')
plt.show()
//Python code
import random
import sys
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib import animation
def roll_dice():
die1 = random.randrange(1, 7)
die2 = random.randrange(1, 7)
return (die1, die2)
def display_dice(dice):
die1, die2 = dice
print(f'Player rolled {die1} + {die2} = {sum(dice)}')
# List that stores number of wins on every roll
winList = []
# List that stores number of losses on every roll
lossList = []
# List that stores label indexes of horizontal bar plot
ylabel = []
# 1
# number of games of craps
n = int(input("Enter number of games: "))
# Iterating 13 times
# Because it is mentioned in the question that plot should have 13
# horizontal bars for wins, and 13 horizontal bars for losses.
for roll in range(13):
ylabel.append('Roll ' + str(roll + 1))
# variables that keep track of wins and losses on every roll
# for the given number of games of craps
wins = losses = 0
for i in range(n):
die_values = roll_dice()
display_dice(die_values)
sum_of_dice = sum(die_values)
if sum_of_dice in (7, 11):
game_status = 'WON'
wins += 1
elif sum_of_dice in (2, 3, 12):
game_status = 'LOST'
losses += 1
Step by step
Solved in 4 steps
Is there a way that you can run the code? And paste the output. Because when ever I run it. It doesn't work.
when ever I run the code it says ax.barh(df.index, df['Wins'], color='blue')
NameError: name 'ax' is not defined. How do I fix it.