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

EBK JAVA PROGRAMMING
8th Edition
ISBN:9781305480537
Author:FARRELL
Publisher:FARRELL
Chapter16: Graphics
Section: Chapter Questions
Problem 8RQ
icon
Related questions
Question

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()

Expert Solution
steps

Step by step

Solved in 4 steps

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

Is there a way that you can run the code? And paste the output. Because when ever I run it. It doesn't work. 

Solution
Bartleby Expert
SEE SOLUTION
Follow-up Question

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.

Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Rendering
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781305480537
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT