Question

In: Computer Science

PYTHON BEGINNER Problem Write a program which, given a number, simulates rolling a pair of six-sided...

PYTHON BEGINNER Problem
Write a program which, given a number, simulates rolling a pair of six-sided dice that number of times. The program should keep track of how many times each possible sum comes up using a list. The list's first element should contain how many times a total of 2 was rolled, the second should contain how many times a total of 3 was rolled, and so on all the way through to 12. When all rolls are complete, the program should output the resulting list as well as a chart as shown in the example below, where the number of asterisks represents the number of times that roll came up (results will vary with each run). In this example, the user requested the dice be rolled 100 times:

Resulting list: [2, 8, 11, 11, 15, 15, 17, 9, 7, 4, 1]                                       

Resulting chart:

2   **                                                                                                           
3   ********                                                                                                     
4   ***********                                                                                                  
5   ***********                                                                                                  
6   ***************                                                                                              
7   ***************                                                                                              
8   *****************                                                                                            
9   *********                                                                                                    
10  *******                                                                                                      
11  ****                                                                                                         
12  *   
Input validation: Do not accept a non-integer input for the number of dice rolls to simulate.
Function decomposition
A function get_number_of_rolls that asks the user for the number of rolls and returns it as an integer. If the user enters a non-integer input, the function should output an error and ask again until a valid input is given (hint: use the isdigit string method.)
A function roll_dice that returns an integer representing the result (sum) of rolling two dice. Import the random module at the top of your program and use the call random.randint(1, 6) to simulate a single six-sided dice.
A function create_histogram that, given an integer number of rolls to simulate, returns a histogram represented as a list of 11 elements with the count of each roll result (i.e. the element at index zero contains the number of times 2 came up, the element at index 1 contains the number of times 3 came up, and so on. See the "Resulting list" in the example above.
A function print_histogram that, given a histogram represented as a list of 11 elements, displays the "Resulting chart histogram to the console as in the example above. Use the "\t" character to print a tab after each number in the line so the asterisks appear aligned.
A main function to drive the program.

Solutions

Expert Solution

Source Code:

import random
def get_number_of_rolls():
n=input("Enter number of rolls: ")
while(n.isdigit()==False):
print("Error, please input a valid positive integer.")
n=input("Enter number of rolls: ")
return n

def roll_dice():
dice1=random.randint(1,6)
dice2=random.randint(1,6)
return (dice1+dice2)

def create_histogram(n):
histogram=[]
rolls=[]
for i in range(0,int(n)):
sum_dice=roll_dice()
rolls.append(sum_dice)
for i in range(2,13):
histogram.append(rolls.count(i))
return histogram

def print_histogram(histogram):
star="*"
for i in range(0,len(histogram)):
print((i+2),"\t",star*histogram[i])


n=get_number_of_rolls()
histogram=create_histogram(n)
print_histogram(histogram)

Sample input and output:


Related Solutions

2. Dice rolling (15 pts) Problem Description: Write a program that rolls a pair of six-sided...
2. Dice rolling (15 pts) Problem Description: Write a program that rolls a pair of six-sided dice, then displays their values sum. • You can use the random method of the Math class to generate a random number for a die like this: (int) (Math.random() * 6) + 1; • The application should display special messages for two ones (snake eyes) and two sixes (box cars). • The application should use static methods (at least two) to organize its code....
Write a program in C++ called RollDice.cpp that simulates rolling a pair of dice until the...
Write a program in C++ called RollDice.cpp that simulates rolling a pair of dice until the total on the dice comes up to be a given number. Ask the user for the number that you are rolling for. To have your program roll two dice, use the rand() function this way: die1 = rand() % 6 + 1; die2 = rand() % 6 + 1; Your program then computes and prints the number of rolls it takes to get the...
Python program that simulates the rolling a die until the number 5 is first obtained. Repeat...
Python program that simulates the rolling a die until the number 5 is first obtained. Repeat the experiment 10,000 times and print out the “Average Number of Rolls to Obtain a Six on a Die”, along with the average value. Include the average number of rolls your program calculates as a comment Please use comments to better explain what is happening
1. Write a program which simulates rolling dice. It should: Display the random number rolled Ask...
1. Write a program which simulates rolling dice. It should: Display the random number rolled Ask the user if they want to roll again. If they don't want to roll again, the program should end. If they do want to roll again, the new number should display. At a minimum, the die should have 6 sides, but if you want to use a d12 or d20, that is fine too. USE PYTHON
Write a program to simulate rolling a six-sided fair die. Allow the user to enter the...
Write a program to simulate rolling a six-sided fair die. Allow the user to enter the number of rolls. Your program should use rand() to get the result of die rolling. Compute the number of occurrences for each number roll. Calculate the percentage for each number roll. Out put the results in the following format. Use a do while loop to allow the user to repeat the process. Here is the sample run for your program: Please enter the number...
. Dice rolling: In c++Write a program that simulates the rolling of two dice. The sum...
. Dice rolling: In c++Write a program that simulates the rolling of two dice. The sum of the two values should then be calculated. [Note: Each die can show an integer value from 1 to 6, so the sum of the two values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 being the least frequent sums.] The following table shows the 36 possible combinations of the two dice. Your program should...
Please write in beginner level PYTHON code! Your job is to write a Python program that...
Please write in beginner level PYTHON code! Your job is to write a Python program that asks the user to make one of two choices: destruct or construct. - If the user chooses to destruct, prompt them for an alternade, and then output the 2 words from that alternade. - If the user chooses construct, prompt them for 2 words, and then output the alternade that would have produced those words. - You must enforce that the users enter real...
An experiment consists of rolling six-sided dice twice.                                  &
An experiment consists of rolling six-sided dice twice.                                      (10) List the sample space for this experiment. Find the probability distribution for this experiment where x represents the number of even numbers in the 2 rolls. Find the mean of the probability distribution. Find the standard deviation of the probability distribution. Would it be unusual to get 2 even numbers? Why or why not? (show your work)
Consider the experiment of rolling a six-sided fair die. Let X denote the number of rolls...
Consider the experiment of rolling a six-sided fair die. Let X denote the number of rolls it takes to obtain the first 5, Y denote the number of rolls until the first 2, and Z denote the number of rolls until the first 4. Numerical answers are needed only for parts (a) and (b). Expressions are sufficient for parts (c), (d), and (e). a) E[X|Y = 1 or Z = 1] b) E[X|Y = 1 and Z = 2] c)...
PYTHON BEGINNER Problem Create a program that lets the user play a simplified game of Blackjack,...
PYTHON BEGINNER Problem Create a program that lets the user play a simplified game of Blackjack, which is played between the user and an automated dealer as follows. The dealer shuffles a standard deck of 52 cards, draws two cards, and gives them to the user. The user can then choose to request another card from the dealer, adding it to their hand. The user can continue to request cards or choose to stop at any time. After each time...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT