Decision Structures Zelle - Chapter 7

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Conditional Execution
CS0007: Introduction to Computer Programming
Conditional Statements Introduction to Computing Science and Programming I.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Computing with Numbers Zelle - Chapter 3 Charles Severance - Textbook: Python Programming: An Introduction to Computer Science, John.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Functions Chapter 4 Python for Informatics: Exploring Information
Conditions in Java. First…Boolean Operators A boolean data type is always true or false. Boolean operators always return true or false For example: (x.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Conditional Execution Chapter 3 Python for Informatics: Exploring Information
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
Python Conditionals chapter 5
Chapter Making Decisions 4. Relational Operators 4.1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
11. EXCEPTION HANDLING Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Control Flow (Python) Dr. José M. Reyes Álamo.
If/Else Statements.
Python Review 1.
More on the Selection Structure
CS0007: Introduction to Computer Programming
COMP 14 Introduction to Programming
Conditional Execution
Introduction to Python
Line Continuation, Output Formatting, and Decision Structures
Making Choices with if Statements
Chapter 4: Making Decisions.
Python: Control Structures
WELCOME to….. The If Statement.
Week 4 - Monday CS 121.
Topics The if Statement The if-else Statement Comparing Strings
Expressions and Control Flow in PHP
Conditional Execution
Ruth Anderson UW CSE 160 Winter 2017
Conditionals (if-then-else)
Topics The if Statement The if-else Statement Comparing Strings
Line Continuation, Output Formatting, and Decision Structures
Selection CIS 40 – Introduction to Programming in Python
Arithmetic operations, decisions and looping
Python - Conditional Execution
Conditional Execution
Loop Structures and Booleans Zelle - Chapter 8
Types, Truth, and Expressions (Part 2)
Ruth Anderson UW CSE 140 Winter 2014
Loops CIS 40 – Introduction to Programming in Python
Conditional Execution
Control flow : if statements
(Oops! When things go wrong)
3. Decision Structures Rocky K. C. Chang 19 September 2018
Introduction to Programming Using Python PART 2
15-110: Principles of Computing
Boolean Expressions to Make Comparisons
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
CHAPTER 5: Control Flow Tools (if statement)
Control Statements.
Presentation transcript:

Decision Structures Zelle - Chapter 7 Charles Severance - www.dr-chuck.com Textbook: Python Programming: An Introduction to Computer Science, John Zelle

One-Way Decisions x = 5 print "Before 5“ if ( x == 5 ) : print "Is 5“ print "Is Still 5“ print "Third 5“ print "Afterwards 5“ print "Before 6“ if ( x == 6 ) : print "Is 6“ print "Is Still 6“ print "Third 6“ print "Afterwards 6" Before 5 Is 5 Is Still 5 Third 5 Afterwards 5 Before 6 Afterwards 6 One-Way Decisions z-203

Several ifs faren = 120 if ( faren > 90) : print "Heat Warning" print "Cold Warning" z-201

Several ifs faren = 120 if ( faren > 90) : print "Heat Warning" print "Cold Warning" z-201

Comparison Operators Boolean expressions using comparison operators evaluate to - True / False - Yes / No Boolean expressions ask a question and produce a Yes or No result which we use to control program flow Comparison operators look at variables but do not change the variables z-203 http://en.wikipedia.org/wiki/George_Boole

Comparison Operators x = 5 if ( x == 5 ) : print "Equals 5“ print "Greater than 4“ if ( x >= 5 ) : print "Greater than or Equal 5“ if ( x < 6 ) :print "Less than 6“ if ( x <= 5 ) : print "Less than or Equal 5“ if ( x != 6 ) : print "Not equal 6" Equals 5 Greater than 4 Greater than or Equal 5 Less than 6 Less than or Equal 5 Not equal 6 z-203

Review Indentation Must increase indent after an if statement or for statement (after : ) Maintain indent to indicate the scope of the block (which lines are affected by the if/for) Reduce indent to back to the level of the if statement or for statement to indicate the end of the block Blank lines are ignored - they can appear anywhere Comments on a line by themselves are ignored

increase / maintain after if or for decrease to indicate end of block blank lines and comment lines ignored x = 5 if x > 2 : # comments print “Bigger than 2” # don’t matter print “Still bigger” # but can confuse you print “Done with 2” # if you don’t line # them up x = 5 if x > 2 : print “Bigger than 2” print “Still bigger” print “Done with 2” for i in range(5) : print i if i > 2 : print “Done with i”, i

Mental begin/end squares x = 5 if x > 2 : # comments print “Bigger than 2” # don’t matter print “Still bigger” # but can confuse you print “Done with 2” # if you don’t line # them up x = 5 if x > 2 : print “Bigger than 2” print “Still bigger” print “Done with 2” for i in range(5) : print i if i > 2 : print “Done with i”, i

Nested Decisions fline = "blah blah" if len(fline) > 1 : yes print “More than one” no fline = "blah blah" if len(fline) > 1 : print "More than one" if fline[0] == 'b' : print "Starts with a b" print “All done” fline[0] == ‘b’ yes no print “Starts with a b” print “All Done”

Nested Decisions fline = "blah blah" if len(fline) > 1 : yes print “More than one” no fline = "blah blah" if len(fline) > 1 : print "More than one" if fline[0] == 'b' : print "Starts with a b" print “All done” fline[0] == ‘b’ yes no print “Starts with a b” print “All Done”

Nested Decisions fline = "blah blah" if len(fline) > 1 : yes print “More than one” no fline = "blah blah" if len(fline) > 1 : print "More than one" if fline[0] == 'b' : print "Starts with a b" print “All done” fline[0] == ‘b’ yes no print “Starts with a b” print “All Done”

Two Way Decisions Sometimes we want to do one thing if a logical expression is true and something else if the expression is false It is like a fork in the road - we must choose one or the other path but not both z-209

Two-way the hard way x > 2 x = 4 if x > 2 : print “Bigger” yes print “Bigger” no x = 4 if x > 2 : print “Bigger” if x <=2 : print “Smaller” x <= 2 yes print “Smaller” no

Two-way using else : x = 4 if x > 2 : print “Bigger” else : no yes x = 4 if x > 2 : print “Bigger” else : print “Not bigger” print “All done” print “Not bigger” print “Bigger” print “All Done” z-209

Two-way using else : x = 4 if x > 2 : print “Bigger” else : no yes x = 4 if x > 2 : print “Bigger” else : print “Smaller” print “All done” print “Smaller” print “Bigger” print “All Done” z-209

Multi-way if x < 2 : print “Small” elif x < 10 : print “Medium” yes x < 2 print “Small” if x < 2 : print “Small” elif x < 10 : print “Medium” else : print “LARGE” print “All done” no x<10 yes print “Medium” no print “LARGE” print “All Done” z-209

Multi-way x = 0 if x < 2 : print “Small” elif x < 10 : yes x < 2 print “Small” x = 0 if x < 2 : print “Small” elif x < 10 : print “Medium” else : print “LARGE” print “All done” no x<10 yes print “Medium” no print “LARGE” print “All Done” z-209

Multi-way x = 5 if x < 2 : print “Small” elif x < 10 : yes x < 2 print “Small” x = 5 if x < 2 : print “Small” elif x < 10 : print “Medium” else : print “LARGE” print “All done” no x<10 yes print “Medium” no print “LARGE” print “All Done” z-209

Multi-way x = 20 if x < 2 : print “Small” elif x < 10 : yes x < 2 print “Small” x = 20 if x < 2 : print “Small” elif x < 10 : print “Medium” else : print “LARGE” print “All done” no x<10 yes print “Medium” no print “LARGE” print “All Done” z-209

Multi-way if x < 2 : print “Small” elif x < 10 : print “Medium” print “Big” elif x< 40 : print “Large” elif x < 100: print “Huge” else : print “Ginormous” # No Else x = 5 if x < 2 : print “Small” elif x < 10 : print “Medium” print “All done” z-209

Multi-way Puzzles if x < 2 : print “Below 2” elif x < 20 : else : print “Something else” Which will never print? if x < 2 : print “Below 2” elif x >= 2 : print “Two or more” else : print “Something else” z-209

The try / except Structure You surround a dangerous section of code with try and except. If the code in the try works - the except is skipped If the code in the try fails - it jumps to the except section z-216

$ cat notry.py astr = "Hello Bob“ istr = int(astr) $ python notry.py Traceback (most recent call last): File "notry.py", line 6, in <module> istr = int(astr) ValueError: invalid literal for int() with base 10: 'Hello Bob' The program stops here All Done z-216

$ cat tryexcept.py astr = "Hello Bob" try: istr = int(astr) except: print "First", istr astr = "123" print "Second", istr When the first conversion fails - it just drops into the except clause and the program continues. $ python tryexcept.py First -1 Second 123 When the second conversion succeeds - it just skips the except clause and the program continues. z-216

try / except astr = "Bob" try: print “Hello” istr = int(astr) print “There” except: istr = -1 print "Done", istr istr = int(astr) print “There” istr = -1 print “Done”, istr Safety net

Sample try/except fname = raw_input("Enter a file name: ") infile = open(fname, "r") print “Blah...” $ python frompart.py Enter a file name: fred Traceback (most recent call last): File "frompart.py", line 7, in <module> infile = open(fname, "r") IOError: [Errno 2] No such file or directory: 'fred'

Sample try/except fname = raw_input("Enter a file name: ") try: infile = open(fname, "r") except: print “File not found”,fname exit() print “Blah...” $ python frompart.py Enter a file name: fred File not found fred $

Another try/except fname = raw_input("Enter a number: ") try: ival = int(rawstr) except: ival = -1 If ival > 0: print “Nice Work” else: print “Not a number” $ python trynum.py Enter a file name:42 Nice work Enter a number:four Not a number $

Summary Multiway decisions using elif Indentation Try / Except to compensate for errors Indentation One Way Decisions Comparison operators == <= >= > < != Nested Decisions Two way Decisions if : and else :