Aren’t there multiple solutions to this exercise? e.g. I used the below without using any not operators:
statement_one = False
statement_two = True
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!”
Yes, but the point of the exercise is to teach what not does, to think about it and apply it. Something like not in or is not may have been better to use Vs a conditional statement where you can just flip a less than / greater than and add an equals to get the same effect as using not.
Hi, the line on 8 and 12 shows “and not” operator, this is never mentioned anywhere. How does this actually work? Also why exactly do i get a syntax error when i try to use “not” only? Thanks!
It also explains they are all left to right, apart from exponentiation.
Back to and not. not flips a boolean (or something that will return a boolean) to the opposite values (true to false and false to true). and compares two booleans to see if they are both true.
As and aside, is not and not in are operators in their own right and are not the same as not by itself. They would be the same as not(a is b) or not(a in b).
I am absolutely stuck on this lesson and am 100% sure I am wrong. This is what I wrote out, Thanks for your help in advance!
def graduation_reqs(gpa, credits):
if (gpa >= 2.0) and (credits >= 120):
return "You meet the requirements to graduate!"
if not (gpa >= 2.0) and (credits >= 120):
return "You do not have enough credits to graduate."
if not (gpa >= 2.0) and (credits >= 120):
return "Your GPA is not high enough to graduate."
if not (gpa >= 2.0) and (credits >= 120):
return "You do not meet either requirement to graduate!"
``
# Everything is properly indented in the code in the lesson but copy and paste removes them. Thanks.
Your if statement below would read in English like so: if gpa is not greater than or equal to 2.0 and credits is greater than or equal to 120, then return, "You do not have enough credits to graduate." That is not correct. The described condition would mean that you don’t have a high enough gpa to graduate, so how could you fix it? What about moving not?
You need it to read: if gpa is greater than or equal to 2.0 and credits is not greater than or equal to 120, then return, "You do not have enough credits to graduate." Write that in code.
The last if statement where neither condition is met is also not correct. If you want not to apply to the entire comparison, you can enclose the whole thing in parenthesis. For example:
>>> False and False
False
>>> not False and False #not False is evaluated first, so we have True and False which is False
False
>>> not (False and False) #by using parenthesis, False and False is evaluated first which is False then not flips it to True
True
Thank you so much for your reply! I guess the part that is confusing me the most is the place of not, if I were to add it the first half, which would be the gpa half, then the not flips the whole statement while if I put it after the *and then it only flips the second half ie. the credits side. Is that correct?