Python - Conditional Execution

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Conditional Execution
Conditional Statements Introduction to Computing Science and Programming I.
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.
Structured programming
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Decision Making George Mason University. Today’s topics 2 Review of Chapter 2: Decision Making Go over exercises Decision making in Python.
Geography 465 Assignments, Conditionals, and Loops.
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
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.
Lecture 10 – Boolean expressions, if statements COMPSCI 101 Principles of Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Copyright 2003 Scott/Jones Publishing 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
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
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.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Decision Making CMSC 201 Chang (rev ).
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 3 Decision Trees Conditionals.
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.
Branching statements.
If/Else Statements.
CS0007: Introduction to Computer Programming
COMP 14 Introduction to Programming
Conditional Execution
Line Continuation, Output Formatting, and Decision Structures
Chapter 4: Making Decisions.
The switch Statement, and Introduction to Looping
Chapter 4: Making Decisions.
Python: Control Structures
Week 4 - Monday CS 121.
Operator Precedence Operators Precedence Parentheses () unary
Python - Functions.
Topics The if Statement The if-else Statement Comparing Strings
If, else, elif.
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
Conditions and Ifs BIS1523 – Lecture 8.
Conditional Execution
Ruth Anderson UW CSE 140 Winter 2014
Loops CIS 40 – Introduction to Programming in Python
Conditional Execution
Control flow : if statements
3. Decision Structures Rocky K. C. Chang 19 September 2018
Selection Statements.
Python for Informatics: Exploring Information
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.
Decision Structures Zelle - Chapter 7
Control Structures.
Presentation transcript:

Python - Conditional Execution

Conditional Steps Yes Program: Output: x = 5 if x < 10: print 'Smaller' Output: x = 5 if x < 10: print 'Smaller’ Smaller Finis Yes X > 20 ? if x > 20: print 'Bigger' print 'Bigger' print 'Finis' print 'Finis'

Comparison Operators -- Boolean expressions produce a Yes or No result, which use to control program flow -- Boolean expressions using comparison operators evaluate to - True / False - Yes / No Python Meaning < Less than <= Less than or Equal == Equal to >= Greater than or Equal > Greater than != Not equal -- Comparison operators look at variables but do not change the variables Remember: “=” is used for assignment.

Comparison Operators x = 5 if x == 5 : print 'Equals 5' if x > 4 : 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

One-Way Decisions x = 5 Yes 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' Yes X == 5 ? Before 5 Is 5 Is Still 5 Third 5 Afterwards 5 Before 6 Afterwards 6 No print 'Is 5' print 'Still 5' print 'Third 5' One-Way Decisions

Indentation -- 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 and comments are ignored - they do not affect indentation

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

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

Nested Decisions x = 42 if x > 1 : print 'More than one' yes Nested Decisions x > 1 print 'More than one' no x = 42 if x > 1 : print 'More than one' if x < 100 : print 'Less than 100' yes x < 100 no print 'Less than 100' print 'All done' print 'All Done'

Nested Decisions x = 42 if x > 1 : print 'More than one' yes Nested Decisions x > 1 print 'More than one' no x = 42 if x > 1 : print 'More than one' yes x < 100 no if x < 100 : print 'Less than 100' print 'Less than 100' print 'All done' print 'All Done'

Nested Decisions x = 42 if x > 1 : print 'More than one' yes Nested Decisions x > 1 print 'More than one' no x = 42 yes x < 100 no print 'Less than 100' if x > 1 : print 'More than one' if x < 100 : print 'Less than 100' print 'All done' print 'All Done'

Two Way Decisions x > 2 no yes -- If a logical expression is true, do something or something else if the expression is false x > 2 print 'Not bigger' print 'Bigger' -- It is like a fork in the road -- we must choose one or the other path but not both print 'All Done'

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

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

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

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

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

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

Multi-way if x < 2 : print 'Small' elif x < 10 : print 'Medium' elif x < 20 : 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'

Multi-way Puzzles if x < 2 : print 'Below 2' elif x < 20 : print 'Below 20' elif x < 10 : print 'Below 10' 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'

The try / except Structure -- 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

$ cat example.py astr = 'Hello ECSU’ istr = int(astr) print 'First', istr astr = '123’ istr = int(astr) print 'Second', istr $ python example.py Traceback (most recent call last): File “example.py", line 2, in <module> istr = int(astr)ValueError: invalid literal for int() with base 10: 'Hello ECSU' All Done

$ cat example.py astr = 'Hello ECSU’ istr = int(astr) $ python example.py Traceback (most recent call last): File "notry.py", line 2, in <module> istr = int(astr)ValueError: invalid literal for int() with base 10: 'Hello ECSU' print 'First', istr astr = '123’ istr = int(astr) The program stops here print 'Second', istr All Done

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

try / except astr = ECSU' print 'Hello' astr = ECSU' 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 rawstr = raw_input('Enter a number:') try: ival = int(rawstr) except: ival = -1 if ival > 0 : print 'Nice work‘ else: print 'Not a number' $ python numex.py Enter a number: 42 Nice work $ python trynumex.py Enter a number: fourtyTwo Not a number $

Exercise: Rewrite your pay program using try and except so that your program handles non-numeric input gracefully. Enter Hours: 20 Enter Rate: nine Error, please enter numeric input Enter Hours: forty