Help with satisfying conditions
Author |
Message |
implosion
|
Posted: Sun Sep 27, 2009 3:31 pm Post subject: Help with satisfying conditions |
|
|
Hi there, i just started learning python and i'm having a bit of difficulty. This is purely beginning stuff but i can't seem to understand why the answer isn't correct.
Question: Given two variables, is_empty of type bool , indicating whether a class roster is empty or not, and number_of_credits of type integer , containing the number of credits for a class, write an expression that evaluates to True if the class roster is not empty and the class is one or three credits.
code: | is_empty == False and number_of_credits == 1 or number_of_credits == 3 |
i'm using Wing IDE |
|
|
|
|
|
Sponsor Sponsor
|
|
|
DtY
|
Posted: Sun Sep 27, 2009 3:58 pm Post subject: RE:Help with satisfying conditions |
|
|
Logical and binds higher than logical or, so say you have the expression
A and B or C
is identical to
(A and B) or C
Not
A and (B or C)
So, you need to use brackets to fix the order of operations,
code: | is_empty == False and (number_of_credits == 1 or number_of_credits == 3) |
Also, it's not required at all, but when using and/or it's suggested you put brackets around each of the operands.
So,
(A == B) or (B == C)
instead of,
A == B or B == C
It just makes it look more clear. |
|
|
|
|
|
implosion
|
Posted: Sun Sep 27, 2009 4:08 pm Post subject: RE:Help with satisfying conditions |
|
|
oh okay. i see. thank you very much. The only other language i know is Turing. lol |
|
|
|
|
|
|
|