Question: Gain experience using NumPy arrays to solve problems. In a modified game of Yahtzee, five eight - sided dice are rolled once. For this assignment,
Gain experience using NumPy arrays to solve problems. In a modified game of Yahtzee, five eightsided dice are rolled once.
For this assignment, you will simulate this modified game of Yahtzee to determine how likely certain outcomes are.
In this modified version of Yahtzee, a Low Roll occurs when each of the five dice is either a or an For example, or
In this modified version of Yahtzee, a Three of a Kind occurs when three of the dice show the same number. The other two dice must not show this number and must also be different from one another. For example, but not
In this modified version of Yahtzee, a Large Straight occurs when the five numbers can be arranged consecutively for example, or Hint: the numpy library contains a sort function. In the section called Class and function definitions, add the missing methods such that when the main is called, it might produce the following output. You will need to write new code, but do not change any of the existing code.
Number of Rolls:
Number of Low Rolls:
Percent:
Number of Three of a Kinds:
Percent:
Number of Large Straights:
Percent:
Yahtzee code:
import numpy as np
import random
class Die:
def initself sides:
A constructor method to create a die"""
self.sides sides
def rollself:
A general method to roll the die"""
return random.randint self.sides
class Yahtzee:
def initself sides:
A constructor method that can record dice rolls"""
self.rolls npzeros dtypenpint
self.sides sides
def rolldiceself:
A general method that rolls dice"""
for i in rangelenselfrolls:
self.rollsi Dieselfsidesroll
def countoutcomesself:
A helper method that determines how many ss etc. were rolled"""
counts npzerosselfsides dtypenpint
for roll in self.rolls:
countsroll
return counts
def mainhowmany:
lowrolls
threeofakinds
largestraights
game Yahtzee # sided dice
for i in rangehowmany:
game.rolldice
if game.isitlowroll:
lowrolls
elif game.isitthreeofakind:
threeofakinds
elif game.isitlargestraight:
largestraights
printNumber of Rolls:", howmany
print
printNumber of Low Rolls:", lowrolls
printPercent::f
formatlowrolls howmany
printNumber of Three of a Kinds:", threeofakinds
printPercent::f
formatthreeofakinds howmany
printNumber of Large Straights:", largestraights
printPercent::fformatlargestraights howmany
n
mainn
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
