python programming
you should use
Here is a quick story about two elephants who live in the Potowatomi Zoo. Every evening the elephants each randomly select one of six sleeping pens to spend the night in. After deciding on which pen, they simultaneously enter the pen from opposite sides. Being slightly vision impaired, they cannot see if the other elephant entered the same pen until it is too late to turn around and go to another pen to spend the night. Thus sometimes they must each spend an uncomfortable night sharing a small sleeping area with another elephant.
After the elephants are asleep the zookeeper randomly picks one of the pens to check. During a break, the zookeeper and the nighttime custodian were talking. The zookeeper told the custodian that 1/3rd of the time when he checks a pen, there's at least one elephant in there, and of the times that there's at least one elephant in the pen, 1/6th of the time the other elephant is in there as well. The custodian said that those percentages cannot be correct, but the zookeeper insisted they are.
Your job is to write a program to simulate this nightly activity 100,000 times to determine if the zookeeper or the custodian is correct. As a simulation, you will program all steps of this process. You are not writing a statistical analysis program to calculate probabilities.
You will report the following statistics.
Your results should be displayed as percentages with two decimal places.
After your results are displayed, you will prompt the user to choose to end the program or run the simulation again. That way, whomever the results indicate is wrong, can easily try another simulation to see if the results were a fluke. You can use a simple message such as:
Run the simulation again? (yes or no):
The full working Code is written here and it is explained with the comments in the program
Python Code:
Code in written form:
import random #to generate random number
def simulate():
error=0.02 #as the error allowed is 2%
oneThird=1/3 #these 3 lines are there to make the range of accepted
values
oneThirdLeft=oneThird-error#left range
oneThirdRight=oneThird+error#ending range
oneSixth=1/6
oneSixthLeft=oneSixth-error
oneSixthRight=oneSixth+error
while True:#to run infinite loop
atLeast1=0 #to keep the count
both=0
for i in range(100000):
firstElephant=random.randint(1,6) #to give the random number from 1
to 6 both included
secondElephant=random.randint(1,6)
zookeeper=random.randint(1,6)
if firstElephant==zookeeper or secondElephant==zookeeper: #if
any one of the pen matches with zookeepers choice ans is correct
Hence Or is used
atLeast1=atLeast1 + 1
if firstElephant==zookeeper and secondElephant==zookeeper:#if
both the elephant are in same pen as the zookeeper guessed and is
useed
both=both + 1
per1=(atLeast1*100)/100000
per2=(both*100)/100000
print("Percentage of time at least 1 elephant is present :%.2f"
% per1 ,"%")
print("Percentage of time both elephant are present :%.2f" % per2 ,
"%")
if (per1>=oneThirdLeft and per2<=oneThirdRight) and
(per2>=oneSixthLeft and per2<=oneSixthRight):#this is used to
check if the percentage calculated is allowed or not
print("Zookeeper is correct ")
else:
print("Custodian is correct")
run=input("Run the simulation again? (yes or no):")
if run=="no":
break
else:
continue
simulate()
Output :
Note: while copying the code kindly check if the formatting is same as the code in picture or not
If you got the Help Then Please UPVOTE this answer
and if you have any doubts please ask in the comments
python programming you should use While loop For loop Range() Break Continue Here is a quick...
Please complete the following: Write a Python script to do the following: 1. Ask the use to enter several sentences, one at a time in a loop). To end the sentence entry, the user enters a blank (empty) sentence. 2. Extract each word from each sentence and put it in a list. This will require at least one loop to go through each sentence in the list. It is up to you how you want to get the words in...
I have to use java programs using netbeans. this course is introduction to java programming so i have to write it in a simple way using till chapter 6 (arrays) you can use (loops , methods , arrays) You will implement a menu-based system for Hangman Game. Hangman is a popular game that allows one player to choose a word and another player to guess it one letter at a time. Implement your system to perform the following tasks: Design...
please use python and provide run result, thank you! click on pic to make it bigger For this assignment you will have to investigate the use of the Python random library's random generator function, random.randrange(stop), randrange produces a random integer in the range of 0 to stop-1. You will need to import random at the top of your program. You can find this in the text or using the online resources given in the lectures A Slot Machine Simulation Understand...
i really need help with the graphs Driving Can Be Dangerous to Your Health: An Interrupted Case Study in Physiology Phil Stephens Department of Biology Villanova University Part 1-The Grandparents Arrive Dave pulled the cell phone out of his pocket, cursing himself for not putting it on vibrate. The children, Jason and Laura, were both asleep, and he knew that the rest of the day would not be fun if they were awakened from their naps. "Hi, Dave. We're just...