How would I use the "not" operator in this exercise?

In this exercise, I’m asked to use the and and not operators inside of a function. Here is a screenshot of the instructions I was given:

However, I wasn’t really sure how to include the not operator. Here is the code that I wrote for this exercise:

def graduation_reqs(gpa, credits):
  if (gpa >= 2.0) and (credits >= 120):
    return "You meet the requirements to graduate!"
  if gpa >= 2.0 and credits < 120:
    return "You do not have enough credits to graduate."
  if gpa < 2.0 and credits >= 120:
    return "Your GPA is not high enough to graduate."
  if gpa < 2.0 and credits < 120:
    return "You do not meet either requirement to graduate!"

I know my code is correct because I received a little checkmark for this exercise, but the instructions did say to use the not operator and I’m just not sure how I would have done that. I don’t think I understand the not operator very well just yet.

Hi @okaraman7,

You could use the not operator in any of the conditional expressions, with some redesign of what is already there, however, the most reasonable places to do it would be in either the second, third, or fourth if block header.

Consider these example functions:

def x_greater_than_y(x, y):
  return x > y

def x_not_greater_than_y(x, y):
  return not x > y

The second function was created by using the not operator to modify the condition that was used in the first function. In a similar manner, you could modify part of this if block header:

  if (gpa >= 2.0) and (credits >= 120):

… to make it appropriate for replacing this block header:

  if gpa >= 2.0 and credits < 120:

Do you think you could give me an example of the not operator in the context of the graduation_reqs function in the exercise? I appreciate your response but I’m still not quite sure how exactly to use the not operator in this exercise. Would it be something along the lines of:

if gpa >= 2.0 and credits not >= 120

Or would it be something like:

if gpa not >= 2.0 and credits >= 120

?

Hello, @okaraman7.

Consider:

if a >= b:
#is the same as:
if not a < b:

So, for a condition checking to see if a student has met the requirements to graduate, you could use not like so:

if not gpa < 2.0 and not credits < 120:
    #You meet requirements code

If gpa is not less than 2.0, then it has to be 2.0 or higher. Same with credits.

That would work for the second condition in your original post. It’s the same as:

if gpa >= 2.0 and credits < 120

Edit: Fixed typo.

1 Like

Hi @okaraman7,

One of your options would be:

  if (gpa >= 2.0) and not (credits >= 120):
    return "You do not have enough credits to graduate."

These two expressions are equivalent:

  • credits < 120
  • not (credits >= 120)
1 Like

@midlindner thank you for providing me with some examples - I definitely understand it better now!

2 Likes

@appylpye thank you for providing me with this example! That was very helpful and I think I understand it now.

1 Like