an introduction to python - problem solving: flowcharts & test cases, boolean variables &...

24
An Introduction To Software Development Using Python Spring Semester, 2014 Class #16: Problem Solving: Flowcharts & Test Cases, Boolean Variables & Operators

Upload: blue-elephant-consulting

Post on 16-Jul-2015

207 views

Category:

Education


1 download

TRANSCRIPT

Page 1: An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean Variables & Operators

An Introduction To Software

Development Using Python

Spring Semester, 2014

Class #16:

Problem Solving:

Flowcharts & Test

Cases, Boolean

Variables & Operators

Page 2: An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean Variables & Operators

What Is A Flowchart?

• A flowchart shows the structure of decisions and tasks that are required to solve a problem.

• When you have to solve a complex problem, it is a good idea to draw a flowchart to visualize the flow of control.

Page 3: An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean Variables & Operators

Flowcharts: The Basic Idea

• Link tasks and input/output boxes in the sequence in which they should be executed.

• Whenever you need to make a decision, draw a diamond with two outcomes.

• Each branch can contain a sequence of tasks and even additional decisions.

Page 4: An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean Variables & Operators

Flowcharts: Multiple Decisions

• If there are multiple choices for a value, use multiple diamonds.

Page 5: An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean Variables & Operators

Avoiding “Spaghetti Code”

• Unconstrained branching and merging can lead to “spaghetti code”.

• Spaghetti code is a messy network of possible pathways through a program.

• There is a simple rule for avoiding spaghetti code: Never point an arrow inside another branch.

Image Credit: beni.hourevolution.org

Page 6: An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean Variables & Operators

Spaghetti Code Example

• Shipping costs are $5 inside the United States.

• Except that to Hawaii and Alaska they are $10.

• International shipping costs are also $10.

Need to add Hawaii &

Alaska shipping…

Page 7: An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean Variables & Operators

Spaghetti Code Example

• You may be tempted to reuse the “shipping cost = $10” task.

• Don’t do that! The red arrow points inside a different branch

Page 8: An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean Variables & Operators

Spaghetti Code Example

• Instead, add another task that sets the shipping cost to $10

Page 9: An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean Variables & Operators

Flowchart Limits

• Flowcharts can be very useful for getting an intuitive understanding of the flow of an algorithm.

• However, they get large rather quickly when you add more details.

• At that point, it makes sense to switch from flowcharts to pseudocode.

Page 10: An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean Variables & Operators

Verifying Code: Test Cases

• How would you test this program?

– Can’t test all possible martial status / income values

• If the program correctly computes one or two tax amounts in a given bracket, then we have good reason to believe that all amounts will be correct.

• You want to aim for complete coverage of all decision points.

Image Credit: theauthorsblogg.wordpress.com

Page 11: An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean Variables & Operators

Create A Testing Plan

• A plan for obtaining a comprehensive set of test cases:

– There are two possibilities for the marital status and two tax brackets for each status, yielding four test cases.

– Test a handful of boundary conditions, such as an income that is at the boundary between two brackets, and a zero income.

– Also test an invalid input, such as a negative income.

Image Credit: www.fotosearch.com

Page 12: An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean Variables & Operators

Planning Your Testing

• Make a list of the test cases and the expected outputs:

Image Credit: www.canstockphoto.com

Page 13: An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean Variables & Operators

Use Flowcharts To Plan Tests

X

X X

X

X

Note: It is always a good idea to design

test cases before starting to code.

Working through the test cases gives

you a better understanding of the

algorithm that you are about to

implement.

Page 14: An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean Variables & Operators

Boolean Variables

• Sometimes, you need to evaluate a logical conditionin one part of a program and use it elsewhere.

• To store a condition that can be true or false, you use a Boolean variable.

• In Python, the bool data type has exactly two values, denoted False and True.

• These values are not strings or integers; they are special values, just for Boolean variables.

• Example:Here is the initialization of a variable set to True:

failed = TrueYou can use the value later in your program to make a decision:

if failed : # Only executed if failed has been set to trueImage Credit: www.sourcecon.com

Page 15: An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean Variables & Operators

Boolean Operators

• When you make complex decisions, you often need to combine Boolean values.

• An operator that combines Boolean conditions is called a Boolean operator.

• In Python, the and operator yields True only when both conditions are true.

• The or operator yields True if at least one of the conditions is true.

Image Credit: www.java-n-me.com

Page 16: An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean Variables & Operators

Boolean Truth Table

Image Credit: www.clipartpanda.com

Page 17: An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean Variables & Operators

Boolean Example: And

• Write a program that processes temperature values.

• You want to test whether a given temperature corresponds to liquid water.

• At sea level, water freezes at 0 degrees Celsius and boils at 100 degrees.

• Water is liquid if the temperature is greater than zero and less than 100:

if temp > 0 and temp < 100 :print("Liquid")

Page 18: An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean Variables & Operators

Boolean Example: Or

if temp <= 0 or temp >= 100 :print("Not liquid")

Page 19: An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean Variables & Operators

Boolean Precedence

• The Boolean operators and and or have a lower precedence than the relational operators.

• For that reason, you can write relational expressions on either side of the Boolean operators without using parentheses.

• For example, in the expression temp > 0 and temp < 100the expressions temp > 0 and temp < 100 are evaluated fist. Then the and operatorcombines the results

Image Credit: www.dreamstime.com

Page 20: An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean Variables & Operators

Invert A Condition

• Sometimes you need to invert a condition with the not Boolean operator.

• The not operator takes a single condition and evaluates to True if that condition is false and to False if the condition is true.

if not frozen :print("Not frozen")

Image Credit: www.clipartpanda.com

Page 21: An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean Variables & Operators

Boolean Operator Examples

Image Credit: industry-illustration.com

Page 22: An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean Variables & Operators

What’s In Your Python Toolbox?

print() math strings I/O IF/Else elif While For

Lists And / Or

Page 23: An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean Variables & Operators

What We Covered Today

1. Flowcharts

2. Creating testing plans

3. Boolean variables

4. Boolean operators

Image Credit: http://www.tswdj.com/blog/2011/05/17/the-grooms-checklist/

Page 24: An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean Variables & Operators

What We’ll Be Covering Next Time

1. Functions, Part 1

Image Credit: http://merchantblog.thefind.com/2011/01/merchant-newsletter/resolve-to-take-advantage-of-these-5-e-commerce-trends/attachment/crystal-ball-fullsize/