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

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
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...
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...
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)
In python please write the following code the problem. Write a function called play_round that simulates...
In python please write the following code the problem. Write a function called play_round that simulates two people drawing cards and comparing their values. High card wins. In the case of a tie, draw more cards. Repeat until someone wins the round. The function has two parameters: the name of player 1 and the name of player 2. It returns a string with format '<winning player name> wins!'. For instance, if the winning player is named Rocket, return 'Rocket wins!'.
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)...
Given a string, such as x = ‘itm330’, write a Python program to count the number...
Given a string, such as x = ‘itm330’, write a Python program to count the number of digits and the number of letters in it. For example, in ‘itm330’, there are 3 letters and 3 digits. Hint: Very similar to page 11 on the slides. To check if a character c is a digit, use c.isdigit(). If c is a digit, c.isdigit() will be a True.
Write a python program that simulates a simple dice gambling game. The game is played as...
Write a python program that simulates a simple dice gambling game. The game is played as follows: Roll a six sided die. If you roll a 1, 2 or a 3, the game is over. If you roll a 4, 5, or 6, you win that many dollars ($4, $5, or $6), and then roll again. With each additional roll, you have the chance to win more money, or you might roll a game-ending 1, 2, or 3, at which...
You will write a program using Python that simulates an Automatic Teller Machine (ATM). For this...
You will write a program using Python that simulates an Automatic Teller Machine (ATM). For this program, your code can have user defined functions (but not required), the program must not call on any external functions or modules to handle any of the input, computational, and output requirements. Requirements: There is a customer with the following credential and can only access the system if the Access Code is correct: • Name: Peter Parker, Access Code: 2222 • When the program...
Rolling a pair of 12 sided dice and summing the numbers find the following: The probability...
Rolling a pair of 12 sided dice and summing the numbers find the following: The probability distribution of the sums. Sums= P(Sums)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT