A either catches his bus with probability 1 - p or misses it with probability p. In the former case, he comes to the office on time, in the latter case he is late. A knows that his boss gets angry if A is late two days in a row. For a given number n of days and given p, A wants to compute the probability that after n days his boss will not be angry.
For example, if n = 2, then the only way to make the boss angry is two be late both days. Since, events of different days are independent, the probability of this event is p*p = p^2.Therefore, the probability of not making the boss angry is 1 - p^2
def probability_not_angry(n, p):
if n == 0:
return 1
if n == 1:
return 1
return probability_not_angry(n-1, p) + probability_not_angry(n-2, p)
# should print 0.5
print(probability_not_angry(4, 0.5))
# should print 0.4375
print(probability_not_angry(2, 0.75))
I am not sure how to solve this problem. if return probability_not_angry(n-1, p) + probability_not_angry(n-2, p), it only calculate the n, so i have to change the p in the function, but now sure how to do it.
Thanks