A Boolean expression with multiple conditions can get quite long. How to keep things concise?

Question

A Boolean expression with multiple conditions can get quite long. How can we keep things concise?

Answer

If you have a long expression with multiple expressions combined with and and or, what you can do is “nest” other if/elif/else statements.

This can be helpful when you want to continue checking other conditions given that some previous condition is met. If the previous condition is not met, then it would not check any further into the nested statements.

Example

# if statement with nested if/else statement
if time == 8:
  if weekend:
    snooze()
  else:
    wakeup()
15 Likes

Can someone help giving other examples to his question?

What sort of example would be helpful? The above given example if fairly concise and describes nesting very well. Do you have any specific scenario in mind?

3 Likes

For the review on control flow, this is how I kept things concise:

def applicant_selector(gpa, ps_score, ec_count):
if (gpa >= 3.0) and (ps_score >= 90):
if (ec_count >= 3):
return “This applicant should be accepted.”
else:
return “This applicant should be given an in-person interview.”
else:
return “This applicant should be rejected.”

Make sure you pay close attention to the indents.

1 Like

Welcome, @eliselehman528451969,

What indents should we be noticing?

Are we going to be able to find this exact example in the forums?

People should note the indents because there are a lot of them. (Ifs
embedded in ifs, and embedded within the function.)

However, we cannot see them to be noted in the post. It’s all left-justified. Seems there is some boilerplate literature given to new users of the forums that may have missed your attention. In it is described how to mark down code samples so they appear as we intend them to. Walking away from code samples that don’t look right is pure irresponsibility.

This is my first post in Codecademy and I am new. I didn’t know.

It would be a good idea to go over the material in the new user section. There is advice on how to post code samples in the forums.

I’ve just started to learn how to code and finished this section but wanted to test out your code to see if it worked any better and it appears to be broken on my end? the code I entered for this module here:

def applicant_selector(gpa, ps_score, ec_count):
  if gpa >= 3.0 and ps_score >= 90 and ec_count >= 3:
    return "This applicant should be accepted."
  # printed if applicant meets all three criteria 
  elif gpa >= 3.0 and ps_score >= 90 and ec_count <= 2:
    return "This applicant should be given an in-person interview."
  else:
    return "This applicant should be rejected."

print(applicant_selector(3.1, 91, 3))
print(applicant_selector(3.1, 91, 2))
print(applicant_selector(3.1, 89, 2))

When I imported your code I received a few errors and even after some trail and error they didnt work for me, but I liked what you did and modified my code to the following

def applicant_selector2(gpa, ps_score, ec_count):
  if gpa >= 3.0 and ps_score >= 90:
    if ec_count >= 3:
      return "This applicant should be accepted."
      # printed if applicant meets all three criteria 
    elif ec_count <= 2:
      return "This applicant should be given an in-person interview."
    else:
      return "This applicant should be rejected."

print(applicant_selector(3.1, 91, 3))
print(applicant_selector(3.1, 91, 2))
print(applicant_selector(3.1, 89, 2))

Instead of two ‘else’ statements, i used ‘elif’ and ‘else’. This definitely saves on re-writing lines, thanks!

Edit: I played around with it a little more and finally figured out how the lines should be indented.


def
  if  
    if 
      return 
    else 
      return 
  else 
    return 

is how it should be.