FAQ: Control Flow - Boolean Operators: and

This community-built FAQ covers the “Boolean Operators: and” exercise from the lesson “Control Flow”.

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

Computer Science
Data Science

FAQs on the exercise Boolean Operators: and

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

Ask or answer a question about this exercise by clicking reply (reply) below!

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

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

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!

4 posts were split to a new topic: Py showing up before expressions

7 posts were split to a new topic: What am I missing in my code?

A post was split to a new topic: How should I involve print in the function?

First expression:

(10<20 and 5<8) and (20>10)

Second expression:

10<20 and 5<8 and 20>10

Are the both expressions similar? Do the Parenthesis effect Boolean expressions just like Mathematical expressions in programming?

Yes, python has order of operations just like math

in your example, the parenthesis do not matter. The outcome is the same, so the parenthesis should be omitted to make it easier to read

1 Like

@stetim94 Thank You for elaborating.

When creating expressions with multiple operators, you can consult this official Python operator precedence table as a guide, in order to make sure that the operations will be performed in the order that you need. It is important to note this opening statement:

The following table summarizes the operator precedence in Python, from lowest precedence (least binding) to highest precedence (most binding).

That statement indicates that the operations listed at the bottom of the table will performed first, with the ones at the top performed last. Notice that parentheses are listed in the last row of the table, indicating that they can be used to control the order of operations in any expression.

Not sure if this is a difference in countries (and grading system) but it seems to me that for the task 2 there is a logic error in the correct result (I only got it right because I tried to do a false statement).

The task was to add a condition to check, if the gpa is correct.
The gpa condition was set, that you need a gpa of 2 or higher.

While (at least to my knowledge) talking from a “higher” gpa (in terms of a better gpa, which is the meaning here as a condition, because I am sure they do not want to have the worst students only to suceed) would be a lower number.

Afaik for the general gpa calculations, 1.0 should be the best and 6.0 the worst. So if we want only 2 gpa or better students to succeed, the statement has to be:

f credits >= 120 and gpa <= 2.0:
  print("You meet the requirements to graduate!")

The exercise however wants this statement (and would deem the other as wrong):

f credits >= 120 and gpa >= 2.0:
  print("You meet the requirements to graduate!")

For me, this is a clear logic error, unless the grading system is the other way around (1 worst, 6 best, like in Switzerland e.g.). Then however a GPA of 2.0 would not make sense to be a criteria for graduating, because it would mean an extremely bad result.

In my opinion (no matter which way you turn it) the results seems rather off (either saying the school is not very good and graduates everyone who can at least write their name right, or the check for the gpa is wrong).

GPA => Grade Point Average

In Canada and USA percentage grades are converted to GPA based on GPA 1.0 being 65%, and GPA 4.0 being 100%.

How to Convert (Calculate) Your GPA to a 4.0 Scale – BigFuture.

I don’t understand why my answer to this exercise is incorrect.

I said that the below statement is False, but codecademy is telling me this statement is True. How can 8 be <= 8? Am I missing something?

less than OR equal to

1 Like

if credits >= 120 and gpa >= 2.0:
is correct,
while
if credits >= 120 and gpa >= 2:
isn’t.

Is this because, you have to add the decimal point, so that python understand that 2 is a floating point numer and it can only compare variables of the same class?

1 Like

Seems you answered your own questions?

In exercise 7, the program only accepts:

if gpa >= 2.0 and credits >= 120:
  print("You meet the requirements to graduate!")

but not:

if  credits >= 120 and gpa >= 2.0:
  print("You meet the requirements to graduate!")

I think it should accept both of them. I spent a good 10 minutes trying to find out what was wrong with my code.

3 Likes

Several of the examples seem to use the factorial function like 5! == 6 #false

How do you enter 5! in the code window, it is giving me a syntax error if I try print(5!)?

There is no symbol for factorial (!) so we need to write our own function, or use the math.factorial() method.

>>> from math import factorial
>>> factorial(5)
120
>>> 
1 Like

Thank you for the response and info!

1 Like

This part was very confusingly worded to me.
Firstly, I had an issue with the task being worded here:

Rewrite the if statement so that it checks to see if a student meets both requirements using an and statement.
If they do, return the string:

I was trying to make a return command work for some time, even trying to research how to make it work. It was good for problem solving but tacked on a needless 20 minutes of trying to figure it out myself. I caved eventually and looked at the “hint” which had the second condition in squared brackets. Ultimately, this combined with other countries using different scoring made it quite unclear? This isn’t the first case of a lesson using the word for a completely different statement, either. Just thought I’d make it known to watch the wording.

Why is this correct
if credits >= 120 and gpa >= 2.0:

but not this?
if (credits >= 120) and (gpa >= 2.0):