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...
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)...
Dice Rolling) Write an application to simulate the rolling of two dice. The application should use...
Dice Rolling) Write an application to simulate the rolling of two dice. The application should use an object of class Random once to roll the first die and again to roll the second die. The sum of the two values should then be calculated. Each die can show an integer value from 1 to 6, so the sum of the values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 being the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT