UK CS 115 - Chapter 7 Decision Structures

Unformatted text preview:

Python Programming: An Introduction to Computer ScienceObjectivesObjectives (cont.)Slide 4Simple DecisionsSlide 6Example: Temperature WarningsSlide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Forming Simple ConditionsSlide 18Slide 19Slide 20Slide 21Slide 22Example: Conditional Program ExecutionSlide 24Slide 25Slide 26Slide 27Slide 28Two-Way DecisionsSlide 30Slide 31Slide 32Slide 33Slide 34Slide 35Slide 36Slide 37Slide 38Slide 39Multi-Way DecisionsSlide 41Slide 42Slide 43Slide 44Slide 45Slide 46Slide 47Slide 48Slide 49Exception HandlingSlide 51Slide 52Slide 53Slide 54Slide 55Slide 56Slide 57Slide 58Slide 59Slide 60Slide 61Study in Design: Max of ThreeSlide 63Strategy 1: Compare Each to AllSlide 65Slide 66Slide 67Slide 68Slide 69Strategy 2: Decision TreeSlide 71Slide 72Slide 73Strategy 3: Sequential ProcessingSlide 75Slide 76Strategy 3: Sequential ProgrammingSlide 78Strategy 4: Use PythonSome LessonsSlide 81Slide 82Slide 83Python Programming, 2/e 1Python Programming:An Introduction toComputer ScienceChapter 7Decision StructuresPython Programming, 2/e 2ObjectivesTo understand the programming pattern simple decision and its implementation using a Python if statement.To understand the programming pattern two-way decision and its implementation using a Python if-else statement.Python Programming, 2/e 3Objectives (cont.)To understand the programming pattern multi-way decision and its implementation using a Python if-elif-else statement.To understand the idea of exception handling and be able to write simple exception handling code that catches standard Python run-time errors.Python Programming, 2/e 4Objectives (cont.)To understand the concept of Boolean expressions and the bool data type.To be able to read, write, and implement algorithms that employ decision structures, including those that employ sequences of decisions and nested decision structures.Python Programming, 2/e 5Simple DecisionsSo far, we’ve viewed programs as sequences of instructions that are followed one after the other.While this is a fundamental programming concept, it is not sufficient in itself to solve every problem. We need to be able to alter the sequential flow of a program to suit a particular situation.Python Programming, 2/e 6Simple DecisionsControl structures allow us to alter this sequential program flow.In this chapter, we’ll learn about decision structures, which are statements that allow a program to execute different sequences of instructions for different cases, allowing the program to “choose” an appropriate course of action.Python Programming, 2/e 7Example:Temperature WarningsLet’s return to our Celsius to Fahrenheit temperature conversion program from Chapter 2.# convert.py# A program to convert Celsius temps to Fahrenheit# by: Susan Computewell def main(): celsius = eval(input("What is the Celsius temperature? ")) fahrenheit = 9/5 * celsius + 32 print("The temperature is", fahrenheit, "degrees Fahrenheit.")main()Python Programming, 2/e 8Example:Temperature WarningsLet’s say we want to modify that program to print a warning when the weather is extreme.Any temperature over 90 degrees Fahrenheit and lower than 30 degrees Fahrenheit will cause a hot and cold weather warning, respectively.Python Programming, 2/e 9Example:Temperature WarningsInput the temperature in degrees Celsius (call it celsius)Calculate fahrenheit as 9/5 celsius + 32Output fahrenheitIf fahrenheit > 90 print a heat warningIf fahrenheit > 30 print a cold warningPython Programming, 2/e 10Example:Temperature WarningsThis new algorithm has two decisions at the end. The indentation indicates that a step should be performed only if the condition listed in the previous line is true.Python Programming, 2/e 11Example:Temperature WarningsPython Programming, 2/e 12Example:Temperature Warnings# convert2.py# A program to convert Celsius temps to Fahrenheit.# This version issues heat and cold warnings.def main(): celsius = eval(input("What is the Celsius temperature? ")) fahrenheit = 9 / 5 * celsius + 32 print("The temperature is", fahrenheit, "degrees fahrenheit.") if fahrenheit >= 90: print("It's really hot out there, be careful!") if fahrenheit <= 30: print("Brrrrr. Be sure to dress warmly")main()Python Programming, 2/e 13Example:Temperature WarningsThe Python if statement is used to implement the decision.if <condition>:<body>The body is a sequence of one or more statements indented under the if heading.Python Programming, 2/e 14Example:Temperature WarningsThe semantics of the if should be clear.First, the condition in the heading is evaluated.If the condition is true, the sequence of statements in the body is executed, and then control passes to the next statement in the program.If the condition is false, the statements in the body are skipped, and control passes to the next statement in the program.Python Programming, 2/e 15Example:Temperature WarningsPython Programming, 2/e 16Example:Temperature WarningsThe body of the if either executes or not depending on the condition. In any case, control then passes to the next statement after the if.This is a one-way or simple decision.Python Programming, 2/e 17Forming Simple ConditionsWhat does a condition look like?At this point, let’s use simple comparisons.<expr> <relop> <expr><relop> is short for relational operatorPython Programming, 2/e 18Forming Simple ConditionsPython MathematicsMeaning< < Less than<= ≤ Less than or equal to== = Equal to>= ≥ Greater than or equal to> > Greater than!= ≠ Not equal toPython Programming, 2/e 19Forming Simple ConditionsNotice the use of == for equality. Since Python uses = to indicate assignment, a different symbol is required for the concept of equality.A common mistake is using = in conditions!Python Programming, 2/e 20Forming Simple ConditionsConditions may compare either numbers or strings.When comparing strings, the ordering is lexigraphic, meaning that the strings are sorted based on the underlying Unicode. Because of this, all upper-case letters come before lower-case letters. (“Bbbb” comes before “aaaa”)Python Programming, 2/e 21Forming Simple ConditionsConditions are based on Boolean expressions, named for the English mathematician George Boole.When a Boolean expression is evaluated, it produces either a value of true (meaning the condition


View Full Document

UK CS 115 - Chapter 7 Decision Structures

Download Chapter 7 Decision Structures
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 7 Decision Structures and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 7 Decision Structures 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?