Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

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
Check Mark
Step 1

//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

Still need help?
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
by 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
by Bartleby Expert
SEE SOLUTION
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
by 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
by Bartleby Expert
SEE SOLUTION
Knowledge Booster
Background pattern image
Computer Science
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
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education