Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help with satisfying conditions
Index -> Programming, Python -> Python Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
implosion




PostPosted: 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
Sponsor
sponsor
DtY




PostPosted: 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




PostPosted: 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
Display posts from previous:   
   Index -> Programming, Python -> Python Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: