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

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...
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...
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!'.
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)
Given n. Write a program in PYTHON to Generate all numbers with number of digits equal...
Given n. Write a program in PYTHON to Generate all numbers with number of digits equal to n, such that the digit to the right is greater than the left digit (ai+1 > ai). E.g. if n=3 (123,124,125,……129,234,…..789)
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)
Write a python program which asks the user to enter a positive number that is greater...
Write a python program which asks the user to enter a positive number that is greater than 30 called, “num2” and then does the following: o 1) Print all numbers between 1 and “num2” that are divisible by 2 and 3. o 2) Print all numbers between 1 and “num2” that are either divisible by 6 or 7. o 3) Print all numbers between 1 and “num3” that is not divisible by 5
Suppose you are rolling a fair four-sided die and a fair six-sided die and you are...
Suppose you are rolling a fair four-sided die and a fair six-sided die and you are counting the number of ones that come up. a) Distinguish between the outcomes and events. b) What is the probability that both die roll ones? c) What is the probability that exactly one die rolls a one? d) What is the probability that neither die rolls a one? e) What is the expected number of ones? f) If you did this 1000 times, approximately...
Write a Python program, in a file called StickFigure.py, which, given a value for the total...
Write a Python program, in a file called StickFigure.py, which, given a value for the total height of a stick figure, uses a recursive function to generate the stick figure pattern as shown below. The recursive function is used to print the entire figure. Note that the head, arms and legs stay the same, but the body can be different lengths, depending on what the user enters as the height of the figure. The "body" length is always 3 lines...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT