Like the last lesson, let's toss two coins, but now look for the chance the results will either be HT or TH.
We'll reuse a function called toss() which handles all of the tossing mechanics of a coin, just returning a 'h' for heads or 't' for tails. We'll put results for coin 1 and coin 2 into their respective variables, then put them together into a third variable called both that will end up being either: hh, tt, ht, or th. We'll count the number of times both comes out to be ht or th.
Now you try. Finish the code, in particular the N= line and the line that looks to see if the toss() function returns an 'h' both times.
Type your code here:
See your results here:
Run the code with N=1000 a few times. What do you see? The chance of seeing the coins as ht or th should be about half (0.5 or 50%). Two ways of thinking about it:
All possible outcomes are HH, HT, TH, or TT. This is 4 possible outcomes. HT or TH is 2 of the 4, with an overall chance of $2/4=0.50$ or 50%.
The chance of one coin showing a H is 0.5 or 50%. The chance of the other coin showing tails is 0.5 or 50%. Thus a single HT combination would be $0.5\times 0.5=0.25$. The chance of a TH is the same or $0.25$. Since we asked for the chance of HT OR TH, we'd add the individual probabilities or $0.25+0.25=0.5$, or 50%. "OR" in probability means add.
Share your code
Show a friend, family member, or teacher what you've done!