Question

Practice: • function • loops • if condition • random Roll a dice many times, you will see that each face value will count ofIn [28]: import random def flip_coin(): x = random.random(). if x > 0.5: return 1 else: return 0 # test the function #print(f

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1 import random 3- def roll_dice(): x = random.random() if x< 1/6: return 1 if x < 2/6: return 2 if x < 3/6: return 3 if x <

CODE

import random

def roll_dice():
x = random.random()
if x < 1/6:
return 1
if x < 2/6:
return 2
if x < 3/6:
return 3
if x < 4/6:
return 4
if x < 5/6:
return 5
return 6

ones = 0
twos = 0
threes = 0
fours = 0
fives = 0
sixes = 0

for i in range(6000):
roll = roll_dice()
if roll == 1:
ones += 1
elif roll == 2:
twos += 1
elif roll == 3:
threes += 1
elif roll == 4:
fours += 1
elif roll == 5:
fives += 1
elif roll == 6:
sixes += 1

print("1s =", ones)
print("2s =", twos)
print("3s =", threes)
print("4s =", fours)
print("5s =", fives)
print("6s =", sixes)

SAMPLE OUTPUT

1s = 1013
2s = 998
3s = 1028
4s = 1007
5s = 992
6s = 962

# Hit the thumbs up if you are fine with the answer. Happy Learning!

Add a comment
Know the answer?
Add Answer to:
Practice: • function • loops • if condition • random Roll a dice many times, you...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a function that flips a coin; it returns heads or tails. Use that function to...

    Write a function that flips a coin; it returns heads or tails. Use that function to write a GetHeadsInARow functions that accepts the number of heads to roll in a row before you return with total rolls it took to get that number of heads in a row. Please Code in Python3 my attempt below will not print out the answer and I do not know why def HeadTails(): from random import randrange """Will simulate the flip of a coin."""...

  • Exercise 10.17. We flip a fair coin. If it is heads we roll 3 dice. If...

    Exercise 10.17. We flip a fair coin. If it is heads we roll 3 dice. If it is tails we roll 5 dice. Let X denote the number of sixes among the rolled dice. (a) Find the probability mass function of X. (b) Find the expected value of X.

  • Write a C program that prompts the user for the number for times the user wants...

    Write a C program that prompts the user for the number for times the user wants to flip a coin. Your program will flip a virtual coin that number of times and return the following: 1) The number of times heads occurred. 2) The number of times tails occured. Then take the coin flip program and modify the random number generator to roll a single 6-sided dice.   DO NOT DISPLAY THE RESULTS OF EACH ROLL. 1) Ask the user how...

  • 3) We roll 2 fair dice. a) Find the probabilities of getting each possible sum (i.e....

    3) We roll 2 fair dice. a) Find the probabilities of getting each possible sum (i.e. find Pr(2), Pr(3), . Pr(12) ) b) Find the probability of getting a sum of 3 or 4 (i.e.find Pr(3 or 4)) c) Find the probability we roll doubles (both dice show the same value). d) Find the probability that we roll a sum of 8 or doubles (both dice show the same value). e) Is it more likely that we get a sum...

  • You flip a fair coin. On heads, you roll two six-sided dice. On tails, you roll...

    You flip a fair coin. On heads, you roll two six-sided dice. On tails, you roll one six-sided dice. What is the chance that you roll a 4? (If you rolled two dice, rolling a 4 means the sum of the dice is 4) O 1 2 3 36 1 2 1 6 + + 1 4 36 1 6 2 2 1 36 + -10 2 . 4 36 + 4 6 2 2

  • Can anyone please answer these? It's python coding question focusing on random practice!! 1. Write a...

    Can anyone please answer these? It's python coding question focusing on random practice!! 1. Write a function biasedCoinFlip(p) to return a biased coin flip that returns ''heads'' with probability p and tails otherwise. For example, when p=0.5, this function will behave like the last. On the other hand, p=0.25 will mean that there is only one chance in four of getting the result of "heads". 2. Write a function randomCard() to randomly return a card in a standard deck(suits: diamonds,...

  • Please help me put this in order!! THANKYOU!! (CO 4 and 6) Create a program with...

    Please help me put this in order!! THANKYOU!! (CO 4 and 6) Create a program with a function named flipCoin that will flip a coin as many times as the user asks. The flipCoin function should return “heads” or “tails”. The program should continue while the user presses Y. SAMPLE OUTPUT: How many times do you want to flip a coin? 5 You got: tails You got: tails You got: tails You got: tails You got: heads To continue press...

  • Problem: Write a function named coinToss that simulates the tossing of a coin. When you call the function, it should ge...

    Problem: Write a function named coinToss that simulates the tossing of a coin. When you call the function, it should generate a random number in the range of 1 through 2. If the random number is 1, the function should display “heads.” If the random number is 2, the function should display “tails.” Demonstrate the function in a program that asks the user how many times the coin should be tossed and then simulates the tossing of the coin that...

  • 1. Roll an even dice and observe the number N on the uppermost face. Then toss...

    1. Roll an even dice and observe the number N on the uppermost face. Then toss a fair coin N times and observe X, the total number of heads that appear in N tosses. (i) Write down the conditional probability mass function pXIN 13) (ii) What is P(X )? (iii) What is E(X)?

  • In C++ please Create a coin-flipping game. Ask the user how many times to flip the...

    In C++ please Create a coin-flipping game. Ask the user how many times to flip the coin, and use the random function to determine heads or tails each time a coin is flipped. Assume the user starts with $50. Every time the coin is flipped calculate the total (heads +$10, tails -$10). Create another function to test if the user has gone broke yet (THIS FUNCTION MUST RETURN A BOOLEAN TRUE/FALSE VALUE). End the program when the user is broke...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT