COSC 235: Programming and Problem Solving
March 20, 2009

Joseph D. Sloan
sloanjd @ wofford.edu


Decisions Tree Coding

Replace each diamond with:

if <test>:
    #right or yes branch
else:
    #left or no branch 

Solution

Modules and Loading

main()
if __name__=='__main__': main()

Example

Exception handling

See example in book
Discover error type and message by creating error in interpreter
E.g.,

>>> a, b, c = 1, 2, 3, 4

Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
a, b, c = 1, 2, 3, 4
ValueError: too many values to unpack

Type is ValueError and message is too many values to unpack

Example

Truth Tables, Boolean Algebra & Boolean Functions

Truth Tables enumerate all possible combinations—be systematic

Parentheses first,
then algebraic expressions,
then the relational operators,
then not,
then and,
then or

Returning True or False

It is okay to return True or False from a function and call the function in a test.
See example.

Truth tables

See example.

Homework

Monday:

P 226: MC

Code decision tree from Friday's homework.

Wednesday:

Add comments to your Energy Density calculator as described below. You should also check input values to insure that they are reasonable. Be sure to correct any existing problems as well including documentation. Submit the code as an email attachment.

Assuming you are trying to lose weight:
If ED is between 0 and 0.6, it is very low and you can eat the food without restraint;
If ED is between 0.6 and 1.5, it is low and you can eat with minor restraints;
If ED is between 1.5 and 4.0, it is medium and you should watch portions;
If ED is between 4.0 and 9.0, it is high and you should closely limit or avoid the food.
Alter your GUI so that it prints appropriate comments based on this information.


This page was created by Joe Sloan.
It was last modified around: 23 March 2009
Send mail to: sloanjd@wofford.edu