FAQ: Control Flow - Review

7 posts were split to a new topic: Why doesn’t my code pass?

2 posts were split to a new topic: My code gives off an error?

3 posts were split to a new topic: Did I make a typo?

A post was split to a new topic: Do booleans need parenthesis?

4 posts were split to a new topic: This was my solution

A post was split to a new topic: Is this correct?

5 posts were split to a new topic: What happens if I nest if statements?

9 posts were split to a new topic: How to correctly use NOT

5 posts were split to a new topic: What are the differences between using if and elif?

In my code, I have this elif statement:

elif (gpa >= 3.0) and (ps_score >= 90) and not (ec_count >= 3):
    return "This applicant should be given an in-person interview."

I’m a bit confused on how this works. I know that elif will only execute its code if the statement to its right it True, but I’m a bit confused on why the statement is that way. When gpa = 3.0, ps_score = 90, ec_count = 2 It is effectively elif True and True and False, which should be False and move on to the next elif statement, so why does it work as if it was elif True?

You’re ignoring part of that expression. The not operator effectively swaps the boolean (not False == True, not True == False). If you break it down a bit-

ec_count = 2
ec_count >= 3  # evaluates to False
not ec_count >= 3  # evaluates to True

I also tried and not (ec_count >= 3) , but when I run it, I get an error saying: “Expected the test values GPA = 3.0, Personal Statement Score = 90, and Extracurricular count = 2 to return the response This applicant should be given an in-person interview., instead it return the response None.”

This is my code:

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."
  elif (gpa >= 3.0) and (ps_score > 90) and not (ec_count >= 3):
    return "This applicant should be given an in-person interview."

Which should return True on all three comparisons, or am I missing something?

A return of None is the default for a function which doesn’t return anything. That should tell you something about the execution of your code. Have a very close look at that error statement and then have a very close look at your conditional statements. If it doesn’t pop out for you then use those exact values and try each conditional separately. Try to avoid guessing and it instead make it entirely certain.

1 Like

Thank you so much! You are so right. I did not take a close enough look at my code and just missed one character there. I am really new to all this and now I have learned to look more precisely to what I think I have typed and what I really have typed. Accuracy…
Thanks again

1 Like

Sounds like you may have figured out the bug in one of your comparisons, so I thought I’d just you a little tip for your not (ec_count >= 3). While this definitely works, we can look at it another way. What is a number that is not greater than or equal to 3? It’d be any number that is less than 3. So we can rewrite not (ec_count >= 3) as ec_count < 3. Both expressions will evaluate the same, but ec_count < 3 is a bit easier to understand when reading and possibly a tiny bit faster, but that depends on how the interpreter reads the code, I’m not sure.

1 Like

Hello I tried going about this exercise a different way and that may have been a mistake in itself. Can someone please tell me where i went wrong? It appears the message “This application should be accepted” should print, but it doesn’t. Please help…

#GPA Fuction

def determine_gpa(gpa_score):

  if gpa_score>=3.0:

    gpa="yes"

  else: 

    gpa="no"

    return gpa

#Personal statement Fuction

def determine_score(score):

  if score >=95:

    ps_score ="yes"

  else:

    ps_score="no"

  return ps_score

#activiteis function

def activity (number):

  if number >=3:

    ec_count="yes"

  else:

    ec_count="no"

    return ec_count

#main

def applicant_selector (gpa, ps_score, ec_count):

  if (gpa=="yes") and (ps_score=="yes") and (ec_count=="yes"):

     message ="This application should be accepted."

  else:

     message ="This application should not be accepted."

     return message

print(applicant_selector(determine_gpa(3.0),determine_score(95),activity(4)))

Have you tried each of these functions as they’re written independently? e.g. determine_gpa(3.0), determine_score… etc. Do they work as expected?

I have not. Let me give that a try now. :slight_smile:

The individual functions did not work…so after looking at the syntax, i realized the indentation was off for each

#GPA Fuction
def determine_gpa(gpa_score):
  if gpa_score>=3.0:
    gpa="yes"
  else:
    gpa="no"
  return gpa

#Personal statement Fuction
def determine_score(score):
  if score >=95:
    ps_score ="yes"
  else:
    ps_score="no"
  return ps_score

#activiteis function
def activity (number):
  if number >=3:
    ec_count="yes"
  else:
    ec_count="no"
  return ec_count


#main
def applicant_selector (gpa, ps_score, ec_count):
  if (gpa=="yes") and (ps_score=="yes") and (ec_count=="yes"):
     message ="This application should be accepted."
  else:
     message ="This application should not be accepted."
  return message

print(applicant_selector(determine_gpa(3.0),determine_score(95),activity(4)))

1 Like

Thank you much!!! :grinning: :smiley:

1 Like