Chapter 8 (Real Python)

Ace your homework & exams now with Quizwiz!

8.8- Suppose you flip a fair coin repeatedly until it lands on both heads and tails at least once each. In other words, after the first flip, you continue to flip the coin until it lands on something different. Doing this generates a sequence of heads and tails. For example, the first time you do this experiment, the sequence might be heads, heads, then tails. On average, how many flips are needed for the sequence to contain both heads and tails? Write a simulation that runs 10,000 trials of the experiment and prints the average number of flips per trial.

Solution: https://github.com/realpython/python-basics-exercises/blob/master/ch08-conditional-logic/8a-challenge-simulate-a-coin-toss-experiment.py

8.9- With some help from the random module and a little condition logic, you can simulate an election between two candidates. Suppose two candidates, Candidate A and Candidate B, are running for mayor in a city with three voting regions. The most recent polls shows that Candidate A has the following chances for winning in each region: Region 1: 87% chance of winning Region 2: 65% chance of winning Region 3: 17% chance of winning Write a program that simulates the election 10,000 times and prints the percentage of where Candidate A wins. To keep things simple, assume that a candidate wins the election is they win at least two of the three regions.

solution A: https://github.com/realpython/python-basics-exercises/blob/master/ch08-conditional-logic/9a-challenge-simulate-an-election.py Solution B: https://github.com/realpython/python-basics-exercises/blob/master/ch08-conditional-logic/9b-challenge-simulate-an-election.py


Related study sets

Penny Ch. 14 - MSK, breast, and superficial structures

View Set

Chapter 9 Pain GOOD with Explanation

View Set

Chapter 32: Concepts of Care for Patients with Cardiac Problems

View Set