FAQ: Rules of Probability - Addition Rule

This community-built FAQ covers the “Addition Rule” exercise from the lesson “Rules of Probability”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Master Statistics with Python

FAQs on the exercise Addition Rule

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi! I find this section confusing. Possibly there is a mistake in the wording of the section? It says “What if we want to find the probability of one or both events occurring? This is the probability of the union of A and B :” but then it goes on to find the probability of one OR another of the events occurring, rather than their intersection or Joint Probability. Then, it seems like it might try in the code to talk about intersection but that never gets explained in the text. Can you help me?

This lesson deals with non-exclusive events so you have probabilities for A or B occurring and a non-zero probability of both A and B occurring at the same time.

It might be the terminology tripping you up here (really wish they used the mathematical symbols in addition to the text).

The phrase you mention specifically mentions the union; “the probability of the union of A and B”. This would be P(AB) that you could also call P(A or B); that is the probability of either A, B or both occurring.

This is different to the probability of both A and B occurring together. That would be the intersection you mention P(AB) or P(A and B).

Yes, thank you!!! I see now how I was reading it wrong, and YES math symbols are WAY better! I get it now.

1 Like

Hi, I’m taking this Fundamental Math Course along with my Data Science Path Courses but I already find it confusing since it requires Python programming in the Addition Rule Quiz and I have no coding experience before. Should I start with another beginner coding course first?

Hi,

I am having a hard time with this part:

print(prob_a_or_b(evens, odds, all_possible_rolls))

Where are evens and odd and all possible rolls defined… as a or b? I see it separate but never combined except on this line which is confusing to me.

Thanks,
Sarah

In the Python script there should be a function definition for prob_a_or_b so the a and b are parameters inside this function that let us calculate the union of these two events occurring.

You could pass them as keyword arguments explicitly if you like but it results in the same function call in the end-
prob_a_or_b(a=evens, b=odds, all_possible_outcomes=all_possible_rolls)

1 Like

Greetings everyone, I am doing the fundament math’s for data science course. I am on rules of probability lesson 5 doing the first exercise which is typing the return statement for the total probability which is ":

return prob_a + prob_b - prob_inter

and I copy the solution and I am getting the error :
File “script.py”, line 15
return prob_a + prob_b - prob_inter
^
SyntaxError: ‘return’ outside function

I have not learnt python as yet and I cant figure out why the code is giving me a error seeing as it is the same code in the solution.

1 Like

Could you link the code you used, without seeing the full function it relies on guesswork, see How do I format code in my posts? for formatted code that keeps indentation (as mentioned below indentation is very important in Python).

A function in Python is a single code block (typically a group of several statements). Python organises code blocks using indentation (generally this is four whitespaces at the start of a line, the cc editor defaults to two, make sure it’s consistent whichever you use). Every statement that is part of the function needs to be indented to the same level.

The following is perfectly valid, each statement that makes up the function is at the same level of indentation-

def func(x):
    y = x + 1
    return y

The following will FAIL because the indentation of the return is wrong (the return is not part of the function code block)-

def func(x):
    y = x + 1
return y  # <-- This will throw a syntax error

Thank you very much I figured out the issue. I should have indented the function, truly sorry I am not used to python code as yet.

1 Like

Bonus question .3
(Event A = 3/6) + (Event B = 4/6) - ( 2/6 Event P (A and B))

Considering event P(A and B):
Rolling a die twice, both being 3 gives a 1/36’th chance of happening. Namely 1/6*1/6
Same goes for rolling a 5 twice, a 1/36’th change of happening.

This is obviously wrong, the answer gives a 1/6’th chance of happening, but why?

What question is this from? There’s no set up to roll a die twice in the linked lesson.

Rules of probability, 5/10, question .3 bonus

" Bonus : Try to calculate the probability using pencil and paper and compare it to the value you get using prob_a_or_b() ."

I’m interpretating it as rolling it twice, because the probability is defined as ‘both events happening’.
Maybe that is the wrong way to interpret it (i’m not native English, sorry in advace).

1 Like

Ah I understand you. I believe it’s not asking about rolling a die twice, it’s asking about rolling it once and either or these events being True (they could both be False, one could be True, the other False, or they could both be True). The final result uses OR logic so that it’s True if either of the events are True.

The important bit to note here is that it’s a single roll and the events themselves are not mutually exclusive (a roll can be both even and greater than 2, e.g. 4 is both even and greater than 2).

So we have one die roll and 6 possibilities-

number A) number > 2 B) number is even A OR B
1 False False False
2 False True True
3 True False True
4 True True True
5 True False True
6 True True True

So 6 events, 5/6 times it’s True.

Edit: Just noticed that the lesson uses odd instead of even but the logic holds (the table would be ever so slightly different). Whilst the table is simple enough for the six-sided die example here knowing how to calculate the result manually (as described in the lesson) is useful.

1 Like

Thank you, my interpretation mistake was:
-interpreting the probabity a and b as seperate rolls of the dice, but in fact, as you said, it is just 1 roll, with not mutually exclusive possibilities

1 Like

hello, I have the same issue with the ‘return outside of function’. I saw your indented solution but it still wont work for me. Was there any thing else you did?

No not really, ensure the indentation or the same. You can also post your code so that others can review it for the issue.