python – Biased coin flip – Stack Overflow

( “ This is answer is based on the latest adaptation of python, sol make sure you are using 3.5.x otherwise some note may not work properly ” )
first your function “ biasedflip ” needs brackets after the name and before the colon for proper note So it should be :

def biasedflip():

second to make it easier to change the probability to use a crop of 1-100 so that you can avoid decimals to make it easier to follow through so when they input ( phosphorus ) it should be 1-100 and that will decide your fail value which is where if it is peer to or higher than it displays Tails You can produce this by using :

random.randint(1,100)

^ This generates a random integer using an inclusive compass of 1-100 preferably than your random.random ( ) which is not going to function.

After editing your random statement you then just change your if statement to look like :

if random.randint(1,100) < Probability:

   print("Heads")

else:

   print("Tails")

When running the program you should besides set the total of flips to be an input so you can test it multiple times rather than editing the code so add a new variable called NumberOfTrials :

NumberOfTrials = int(input("How many times do you wish to flip the coin? "))

^ This converts their stimulation into an integer so it can be used in the range function
With the input signal for your probability python takes standard inputs to be strings so you need to convert the input into an integer which you can do by using :

Probability = int(input("Enter a probability for heads between 1 and 100"))

^This will convert the input signal straight to an integer and assign it to Probability, I have changed the variable identify from `` phosphorus '' to `` Probability '' so the use is clearly defined

At this stage from the top of your program it should look like :

import random

i = 0
Probability = int(input("Enter a probability for heads between 1 and 100: "))
NumberOfTrials = int(input("How many times do you wish to flip the coin? "))

def biasedflip():

   if random.randint(1,100) < Probability:

      print("Heads")

   else:

      print("Tails")

Next we will add another 2 new variables so you can store the totals for Heads and Tails individually to allow for subsequently reference book :

TotalHeads = 0
TotalTails = 0 

^These are starting as 0 and will be defined before the officiate so they are not reset every time we call it, only every time the program is reset
The final examination stage is adding a loop and then displaying the total number of heads and tails. This can be done using a while loop and a count variable star of ( one ) which you have already defined before and set to 0. This will something like :

while i < NumberOfTrials:
   biasedflip()
   i += 1

^After adding this closed circuit we need to modify our affair so that the TotalHeads and TotalTails variables are increased inside the serve rather than complicating it and editing at heart and outside of the function. This just involves adding a TotalHeads += 1 below the Print ( `` Heads '' ) statement and a TotalTails += 1 below the Print ( `` Tails '' ) statement. An important note is as we have set the variables outside of the function we have to had 2 ball-shaped statements to call the variables in to the affair to allow edit, these statements just look like global TotalHeads and global TotalTails and should be the first commands of the affair.

We besides need to add a final examination print command at the end just displaying the totals therefore our concluding consequence should look like :

import random

TotalHeads = 0
TotalTails = 0
i = 0
Probability = int(input("Enter a probability for heads between 1 and 100: "))
NumberOfTrials = int(input("How many times do you wish to flip the coin? "))

def biasedflip():

   global TotalTails
   global TotalHeads

   if random.randint(1,100) < Probability:

      print("Heads")
      TotalHeads += 1

   else:

      print("Tails")
      TotalTails += 1

while i < NumberOfTrials:
   biasedflip()
   i += 1

print("After {0} flips there was {1} Head(s) and {2} Tail(s)".format(NumberOfTrials,TotalHeads,TotalTails))

^You can see on that final instruction I have used .format which means you can use placeholder { 0 } values to fill with variables such as NumberOfTrials so you can make sentences editable through the broadcast and look neat when printed
besides for calling functions the general format is FunctionName() but when defining it should be def FunctionName():

Related Posts

Leave a Reply

Your email address will not be published.