SlideShare ist ein Scribd-Unternehmen logo
1 von 24
An Introduction To Software
Development Using Python
Spring Semester, 2014
Class #16:
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.
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.
Flowcharts: Multiple Decisions
• If there are multiple
choices for a value, use
multiple diamonds.
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
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…
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
Spaghetti Code Example
• Instead, add another task that sets the
shipping cost to $10
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.
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
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
Planning Your Testing
• Make a list of the test cases and the expected
outputs:
Image Credit: www.canstockphoto.com
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.
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
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
Boolean Truth Table
Image Credit: www.clipartpanda.com
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")
Boolean Example: Or
if temp <= 0 or temp >= 100 :
print("Not liquid")
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
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
Boolean Operator Examples
Image Credit: industry-illustration.com
What’s In Your Python Toolbox?
print() math strings I/O IF/Else elif While For
Lists And / Or
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/
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/

Weitere ähnliche Inhalte

Ähnlich wie An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean Variables & Operators

classVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptxclassVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptx
ssusere336f4
 
Java developer trainee implementation and import
Java developer trainee implementation and importJava developer trainee implementation and import
Java developer trainee implementation and import
iamluqman0403
 
classVI_Coding_Teacher_Presentation.pptx
classVI_Coding_Teacher_Presentation.pptxclassVI_Coding_Teacher_Presentation.pptx
classVI_Coding_Teacher_Presentation.pptx
ssusere336f4
 
INTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGE
INTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGEINTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGE
INTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGE
RathnaM16
 

Ähnlich wie An Introduction To Python - Problem Solving: Flowcharts & Test Cases, Boolean Variables & Operators (20)

Testing
TestingTesting
Testing
 
Cse115 lecture03problemsolving
Cse115 lecture03problemsolvingCse115 lecture03problemsolving
Cse115 lecture03problemsolving
 
Intro To C++ - Class 12 - For, do … While
Intro To C++ - Class 12 - For, do … WhileIntro To C++ - Class 12 - For, do … While
Intro To C++ - Class 12 - For, do … While
 
classVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptxclassVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptx
 
An Introduction To Python - WHILE Loop
An Introduction To  Python - WHILE LoopAn Introduction To  Python - WHILE Loop
An Introduction To Python - WHILE Loop
 
Test Coverage: An Art and a Science
Test Coverage: An Art and a ScienceTest Coverage: An Art and a Science
Test Coverage: An Art and a Science
 
Intro To C++ - Class 10 - Control Statements: Part 2
Intro To C++ - Class 10 - Control Statements: Part 2Intro To C++ - Class 10 - Control Statements: Part 2
Intro To C++ - Class 10 - Control Statements: Part 2
 
An Introduction To Software Development - Final Review
An Introduction To Software Development - Final ReviewAn Introduction To Software Development - Final Review
An Introduction To Software Development - Final Review
 
Introduction to C ++.pptx
Introduction to C ++.pptxIntroduction to C ++.pptx
Introduction to C ++.pptx
 
Python-Certification-Training-Day-1-2.pptx
Python-Certification-Training-Day-1-2.pptxPython-Certification-Training-Day-1-2.pptx
Python-Certification-Training-Day-1-2.pptx
 
'A critique of testing' UK TMF forum January 2015
'A critique of testing' UK TMF forum January 2015 'A critique of testing' UK TMF forum January 2015
'A critique of testing' UK TMF forum January 2015
 
SE UNIT 5 part 2 (1).pptx
SE UNIT 5 part 2 (1).pptxSE UNIT 5 part 2 (1).pptx
SE UNIT 5 part 2 (1).pptx
 
2.2 Demonstrate the understanding of Programming Life Cycle
2.2 Demonstrate the understanding of Programming Life Cycle2.2 Demonstrate the understanding of Programming Life Cycle
2.2 Demonstrate the understanding of Programming Life Cycle
 
Intro To C++ - Class 09 - Control Statements: Part 1
Intro To C++ - Class 09 - Control Statements: Part 1Intro To C++ - Class 09 - Control Statements: Part 1
Intro To C++ - Class 09 - Control Statements: Part 1
 
Entity matching of web offers, from html to similarity score.
Entity matching of web offers, from html to similarity score. Entity matching of web offers, from html to similarity score.
Entity matching of web offers, from html to similarity score.
 
Java developer trainee implementation and import
Java developer trainee implementation and importJava developer trainee implementation and import
Java developer trainee implementation and import
 
classVI_Coding_Teacher_Presentation.pptx
classVI_Coding_Teacher_Presentation.pptxclassVI_Coding_Teacher_Presentation.pptx
classVI_Coding_Teacher_Presentation.pptx
 
INTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGE
INTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGEINTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGE
INTRODUCTION TO CODING-CLASS VI LEVEL-DESCRIPTION ABOUT SYNTAX LANGUAGE
 
linear optimization.pptx
linear optimization.pptxlinear optimization.pptx
linear optimization.pptx
 
Web technologies-course 08.pptx
Web technologies-course 08.pptxWeb technologies-course 08.pptx
Web technologies-course 08.pptx
 

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 

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.
  • 4. Flowcharts: Multiple Decisions • If there are multiple choices for a value, use multiple diamonds.
  • 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
  • 8. Spaghetti Code Example • Instead, add another task that sets the shipping cost to $10
  • 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
  • 16. Boolean Truth Table Image Credit: www.clipartpanda.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")
  • 18. Boolean Example: Or if temp <= 0 or temp >= 100 : print("Not 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
  • 21. Boolean Operator Examples Image Credit: industry-illustration.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

  1. 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.