Question

Python Practice Sample: Write a program to simulate rolling a 6-sided die until the sum of...

Python Practice Sample:

Write a program to simulate rolling a 6-sided die until the sum of the numbers rolled is equal to or greater than 21. If it is exactly 21, print “You won!”; if it is more than 21, print “You lost!.” Show the number rolled and the accumulated total each time.

Number rolled: 6. Your total is now: 6.

Number rolled: 6. Your total is now: 12.

Number rolled: 4. Your total is now: 16.

Number rolled: 1. Your total is now: 17.

Number rolled: 6. Your total is now: 23.

You lost.

Homework Answers

Answer #1
from random import randint

total = 0
while total < 21:
    n = randint(1, 6)
    total += n
    print("Number rolled: {}. Your total is now: {}.".format(n, total))
if total == 21:
    print("You won!")
else:
    print("You lost!")


Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Imagine rolling two fair 6 sided dice. the number rolled on the first die is even...
Imagine rolling two fair 6 sided dice. the number rolled on the first die is even and the sum of the rolls is ten. are these two events independent?
For the following questions, find the probability using a standard 6-sided die or two 6-sided dice....
For the following questions, find the probability using a standard 6-sided die or two 6-sided dice. Write your answer as a fraction or with a colon in lowest terms. Rolling a single die, what is the probability of rolling an even number? Rolling a single die, what is the probability of rolling a 5? Rolling a single die, what is the probability of rolling a 7? Rolling a single die, what is the probability of rolling a number less than...
Imagine rolling a fair 6-sided die until we get a six. We know that the probability...
Imagine rolling a fair 6-sided die until we get a six. We know that the probability that this occurs on the nth roll is (5/6)n−1·(1/6). Now describe: - the infinite sample space of the experiment - the probability function for this experiment - Show that your probability function satisfies Pr(Ω) =1 Describe how you obtained your answers.
A game involves rolling a fair six-sided die. If the number obtained on the die is...
A game involves rolling a fair six-sided die. If the number obtained on the die is a multiple of three, the player wins an amount equal to the number on the die times $20. If the number is not a multiple of three, the player gets nothing. How will you model the simulation for the roll of a die? A. Use the numbers 1–20 to represent the numbers rolled when a player wins. B. Use the numbers 1–6 to represent...
you will simulate rolling ?n dies and recording their sum. For example, if n=2 and the...
you will simulate rolling ?n dies and recording their sum. For example, if n=2 and the first die shows up as a 3, and the second die shows up as a 1, the sum (and the value we record) would be 4. TO DO: Complete the two function stubs below and then demonstrate by providing code which would print out the probability distribution for rolling 2 dice 100,000 times. seed(0)    def roll_and_add_dice(num_dice, num_trials = 10**6): pass # Your code...
1. Suppose you have a fair 6-sided die with the numbers 1 through 6 on the...
1. Suppose you have a fair 6-sided die with the numbers 1 through 6 on the sides and a fair 5-sided die with the numbers 1 through 5 on the sides. What is the probability that a roll of the six-sided die will produce a value larger than the roll of the five-sided die? 2. What is the expected number of rolls until a fair five-sided die rolls a 3? Justify your answer briefly.
5. Suppose the six-sided die you are using for this problem is not fair. It is...
5. Suppose the six-sided die you are using for this problem is not fair. It is biased so that rolling a 6 is three times more likely than any other roll. For this problem, the experiment is rolling a six-sided die twice. (A): What is the probability that one or both rolls are even numbers (2, 4 or 6’s)? (B): What is the probability that at least one of the rolls is an even number or that the sum of...
A die is rolled repeatedly until the sum of the numbers obtained islarger than 200. What...
A die is rolled repeatedly until the sum of the numbers obtained islarger than 200. What is the probability that you need more than 66 rolls to do this?Hint: If X is the number of dots showing on the face of a die, E[X] = 7/2 and Var(X)= 35/12.
PROBLEM #2 Suppose you play a game in which a fair 6 sided die is rolled...
PROBLEM #2 Suppose you play a game in which a fair 6 sided die is rolled once. If the outcome of the roll (the number of dots on the side facing upward) is less than or equal to 4, you are paid as many dollars as the number you have rolled. Otherwise, you lose as many dollars as the number you have rolled. Let X be the profit from the game (or the amount of money won or lost per...
2. Consider a ten-sided die of which the sides display the numbers 1, 2, 3, and...
2. Consider a ten-sided die of which the sides display the numbers 1, 2, 3, and 4 according to this table: side of die 1 2 3 4 5 6 7 8 9 10 number displayed 1 1 1 1 2 2 2 3 3 4 Rolling two such dice is an experiment with the sample space S =       (1,1) (1,2) (1,3) (1,4) (2,1) (2,2) (2,3) (2,4) (3,1) (3,2) (3,3) (3,4) (4,1) (4,2) (4,3)...