Selection: Non-linear programs

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Making Decisions in Python Sec 9-10 Web Design. Objectives The student will: Understand how to make a decision in Python Understand the structure of an.
Conditional Statements Introduction to Computing Science and Programming I.
Running JavaScript Chapter 18.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Lecture 11 – if … else, if... elif statements, nested ifs COMPSCI 1 1 Principles of Programming.
Chapter 9 IF Statement Bernard Chen. If Statement The main statement used for selecting from alternative actions based on test results It’s the primary.
Chapter 9 IF Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
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.
Introduction to Computing Using Python Imperative Programming  what is a Python program?  print() statement  input() statement  type conversion statements.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 7 Conditionals and Loops 4/18/09 Python Mini-Course: Day 2 - Lesson 7.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
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.
3.1.3 Program Flow control Structured programming – SELECTION in greater detail.
These Guys? Wait, What? Really?  Branching is a fundamental part of programming  It means taking an action based on decision  The decision is dependent.
If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to.
Control Structures If Else Statement. S E S Q C E N U …consecutive steps (or groups of steps) processed one after another in the order that they arise.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Control Structures WHILE Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
 Type Called bool  Bool has only two possible values: True and False.
Introduction to Python Selection Non-Linear Programs.
PYTHON PROGRAMMING Year 9. Objective and Outcome Teaching Objective Today we will look at conditional statements in order to understand how programs can.
Introduction to Decision Structures and Boolean Variables
Control Flow (Python) Dr. José M. Reyes Álamo.
Line Continuation, Output Formatting, and Decision Structures
Sequence, Selection, Iteration The IF Statement
Lesson 04: Conditionals Topic: Introduction to Programming, Zybook Ch 3, P4E Ch 3. Slides on website.
Upsorn Praphamontripong CS 1110 Introduction to Programming Fall 2016
Selection and Python Syntax
Introduction to Programming
Summary Conditional: if .. else New Nested conditional elif
Topics The if Statement The if-else Statement Comparing Strings
If, else, elif.
CSc 110, Spring 2017 Lecture 8: input; if/else
Imperative Programming
Topics The if Statement The if-else Statement Comparing Strings
Line Continuation, Output Formatting, and Decision Structures
Introduction to Programming
And now for something completely different . . .
Program Flow Control Selection & repetition
Introduction to Programming
3. Decision Structures Rocky K. C. Chang 19 September 2018
Visual Basic – Decision Statements
Selection Statements.
Comparing Strings Strings can be compared using the == and != operators String comparisons are case sensitive Strings can be compared using >, =, and.
Introduction to Decision Structures and Boolean Variables
Conditional and iterative statements
Adding Intelligence Check for the presence or absence of a condition in the environment Take the appropriate actions Involves making a choice (to do or.
Topics The if Statement The if-else Statement Comparing Strings
Learning Intention I will learn about programming using selection (making choices) with one condition.
The if Statement Control structure: logical design that controls order in which set of statements execute Sequence structure: set of statements that execute.
PYTHON: BUILDING BLOCKS Sequencing & Selection
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
CHAPTER 5: Control Flow Tools (if statement)
Chapter 3: Selection Structures: Making Decisions
Control Statements.
Relational and Logical Operators
Introduction to Programming
Relational and Logical Operators
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Imperative Programming
GCSE Computing.
GCSE Computing.
Flow Control I Branching and Looping.
Presentation transcript:

Selection: Non-linear programs Introduction to Python

Learning objectives Use selection statements if, else and elif in a program Learn how to use different comparison operators Use indentation correctly to define a block of code

What exactly does this program do? #Password Checker print("Welcome to PGO Security Systems") print("*******************************") password = input("Enter your password: ") if password == "abcd1234": print("Access Granted") else: print("Access Denied") print("Press ENTER to exit the program")

Indentation! if password == "abcd1234": print("Access Granted") else: Python requires indentation as part of the syntax Indentation signifies the start and end of a block of code Programs will not run without correct indentation if password == "abcd1234": print("Access Granted") else: print("Access Denied") print("Press ENTER to exit the program")

Using an IF statement might get away with it Else busted An IF Statement uses Selection IF forces a decision If condition Then do this Else do that If nobody saw me do it Then might get away with it Else busted

IF syntax age = int(input("Enter age: ")) if age >= 18: print ("Adult") else: print ("Child")

Greater than or equal to Comparison operators Operator Meaning Example Evaluates to == equal to 7==7 True != not equal to 6!=7 > Greater than 7>6 < Less than 5<8 >= Greater than or equal to 6>=8 False <= Less than or equal to 7<=7

Who uses IF statements? Thinking of a mobile phone, for example, where might an IF Statement appear in its coding?

Write a program Write a simple program that might be used inside a police speed camera The Requirements are: It must accept the driver’s speed IF the speed is over 70mph, a message “Issue Fine” should appear on the speed gun Otherwise it should display “No Action”

The ELIF statement if grade >= 80: print("Distinction") If gives you two options ELIF stands for Else, If and gives you more options You can use it as many times as you like if grade >= 80: print("Distinction") elif grade >= 70: print("Merit") elif grade >= 60: print("Pass") else: print("Fail")

Using the ELIF statement Add an ELIF statement to the Speeding program Issue a warning between 70 and 75mph Only issue a fine for 75mph and over