This presentation is a part of the COP2271C college level course taught at the Florida Polytechnic University located in Lakeland Florida. The purpose of this course is to introduce Freshmen students to both the process of software development and to the Python language.
The course is one semester in length and meets for 2 hours twice a week. The Instructor is Dr. Jim Anderson.
A video of Dr. Anderson using these slides is available on YouTube at:
http://youtu.be/O95fDKGIrrM
An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean Variables & Operators
1. An Introduction To Software
Development Using Python
Spring Semester, 2014
Class #16:
Problem Solving:
Flowcharts & Test
Cases, Boolean
Variables & Operators
2. 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.
3. 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.
5. 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
6. 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…
7. 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
9. 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.
10. 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
11. 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
12. Planning Your Testing
• Make a list of the test cases and the expected
outputs:
Image Credit: www.canstockphoto.com
13. 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.
14. Boolean Variables
• Sometimes, you need to evaluate a logical condition
in 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 = True
You can use the value later in your program to make a decision:
if failed : # Only executed if failed has been set to true
Image Credit: www.sourcecon.com
15. 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
17. 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")
19. 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 < 100
the expressions temp > 0 and temp < 100
are evaluated fist. Then the and operator
combines the results
Image Credit: www.dreamstime.com
20. 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
22. What’s In Your Python Toolbox?
print() math strings I/O IF/Else elif While For
Lists And / Or
23. 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/
24. 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/
Hinweis der Redaktion
New name for the class
I know what this means
Technical professionals are who get hired
This means much more than just having a narrow vertical knowledge of some subject area.
It means that you know how to produce an outcome that I value.
I’m willing to pay you to do that.