Decision Structures: Selection Statements - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Decision Structures: Selection Statements

Description:

... 'The temperature is', fahrenheit, 'degrees fahrenheit.' if fahrenheit 90: ... print 'OK, so you won the Anderson Honorary Pocket Protector.' else: print 'Sorry. ... – PowerPoint PPT presentation

Number of Views:15
Avg rating:3.0/5.0
Slides: 17
Provided by: Cyn54
Category:

less

Transcript and Presenter's Notes

Title: Decision Structures: Selection Statements


1
Decision Structures Selection Statements
2
What Are Decision Structures?
  • Code tools which allow the program to perform
    different tasks for different conditionsin
    effect, the code chooses a set of actions when
    certain requirements are met.
  • Increases complexity and flexibility in the
    sequential execution of programs
  • Part of a larger set of programming protocols
    known as Control Structures

3
Temperature Example
Input Celsius Convert to Fahrenheit
Fahrenheit gt 90?
Condition
yes
Print heat warning
Fahrenheit lt 30?
Condition
yes
Print cold warning
4
Convert2.py
  • convert2.py
  • A program to convert Celsius temps to
    Fahrenheit.
  • This version issues heat and cold warnings.
  • def main()
  • celsius input("What is the Celsius temperature?
    ")
  • fahrenheit 9.0 / 5.0 celsius 32
  • print "The temperature is", fahrenheit, "degrees
    fahrenheit."
  • if fahrenheit gt 90
  • print "It's really hot out there, be
    careful!"
  • if fahrenheit lt 30
  • print "Brrrrr. Be sure to dress warmly"
  • main()

5
Relational Operators
6
Boolean Data Types
  • When an expression evaluates as true or false, we
    call it a Boolean statement.
  • Python (like many languages) has a Boolean data
    type. Some languages interpret a true as 1 or -1
    and a false as 0. In Python, a Boolean data type
    is called bool and is represented simply as True
    or False.
  • gtgtgt 3 4 lt 3 4False

7
A one-way decision structure
  • quadratic2.py
  • A program that computes the real roots of a
    quadratic equation.
  • Bad version using a simple if to avoid
    program crash
  • import math
  • def main()
  • print "This program finds the real solutions
    to a quadratic\n"
  • a, b, c input("Please enter the
    coefficients (a, b, c) ")
  • discrim b b - 4 a c
  • if discrim gt 0
  • discRoot math.sqrt(discrim)
  • root1 (-b discRoot) / (2 a)
  • root2 (-b - discRoot) / (2 a)
  • print "\nThe solutions are", root1,
    root2
  • main()

8
Two-way Decisions (If-Else)
  • Using just the if keyword provides us with a
    single alternative and, implicitly, a second
    alternativewhat to do if the if condition is NOT
    met. But this is not very clear or satisfactory
    as good programming.
  • Python provides us with a structure that covers
    all exceptions to the condition linked with the
    if keyword.
  • if ltconditiongt (note the colons remember
    indentation) ltstatementsgtelse ltstatement
    sgt

9
If..else with the quadratics program
  • quadratic3.py
  • A program that computes the real roots of a
    quadratic equation.
  • Illustrates use of a two-way decision
  • import math
  • def main()
  • print "This program finds the real solutions
    to a quadratic\n"
  • a, b, c input("Please enter the
    coefficients (a, b, c) ")
  • discrim b b - 4 a c
  • if discrim lt 0
  • print "\nThe equation has no real roots!"
  • else
  • discRoot math.sqrt(b b - 4 a c)
  • root1 (-b discRoot) / (2 a)
  • root2 (-b - discRoot) / (2 a)
  • print "\nThe solutions are", root1,
    root2

10
Another if else example
  • gpa_award.pyimport math
  • def main() print This program determines
    student awards\n GPA float(input(Enter the
    students GPA)) if GPA gt 3.8 print
    Congratulations! You won the Mitchell
    scholarship! else print Sorry. Your GPA
    does not qualify for an award.main()

11
Multiple Decisions (if elif)
  • Sometimes there are multiple choices, not just
    two. In these cases we want to nest if
    statements inside each other
  • 3.8 GPA and above gets the Mitchell scholarship
  • But if you dont qualify for that and still have
    a GPA of 3.5 or better, you get the Wilson award
  • But if you have a GPA between 3.0 and 3.5, you
    may qualify for the Anderson Recognition Honorary
    Pocket Protector

12
Example GPAs and awards
  • gpa_award.pyimport math
  • def main() print This program determines
    student awards\n GPA float(input(Enter the
    students GPA)) if GPA gt 3.8 print
    Congratulations! You won the Mitchell
    scholarship! elif GPA gt 3.5 and GPA lt
    3.8 print Congrats! You won the Wilson
    award! elif GPA gt 3.0 and GPA lt 3.5 print
    OK, so you won the Anderson Honorary Pocket
    Protector. else print Sorry. Your GPA does
    not qualify for an award.main()

13
Back to Quadratics
  • quadratic4.py
  • A program that computes the real roots of a
    quadratic equation.
  • Illustrates use of a multi-way decision
  • import math
  • def main()
  • print "This program finds the real solutions
    to a quadratic\n"
  • a, b, c input("Please enter the
    coefficients (a, b, c) ")
  • discrim b b - 4 a c
  • if discrim lt 0
  • print "\nThe equation has no real roots!"
  • elif discrim 0
  • root -b / (2 a)
  • print "\nThere is a double root at", root
  • else
  • discRoot math.sqrt(b b - 4 a c)

14
Exception Handling
  • A lot can go wrong with programs, mainly because
    either a) the programmer made a logical or syntax
    error b) the programmer didnt give proper
    instructions to the user or c) the user is
    difficult or just stupid.
  • When programmers can predict the kinds of errors
    that might arise from user input, she/he can
    write in ways of dealing with itusually giving
    new instructions to the user. The program can
    handle exceptions or handle errors.
  • Python provides a special syntax for exception
    handling. try ltstatementsgtexcept
    ltErrorTypegt lterror handling statementsgt

15
Handling Exceptions in the Quadratic program
  • quadratic5.py
  • A program that computes the real roots of a
    quadratic equation.
  • Illustrates exception handling to avoid
    crash on bad inputs
  • import math
  • def main()
  • print "This program finds the real solutions
    to a quadratic\n"
  • try
  • a, b, c input("Please enter the
    coefficients (a, b, c) ")
  • discRoot math.sqrt(b b - 4 a c)
  • root1 (-b discRoot) / (2 a)
  • root2 (-b - discRoot) / (2 a)
  • print "\nThe solutions are", root1,
    root2
  • except ValueError
  • print "\nNo real roots"
  • main()

16
Exception Handling in the GPA example
  • gpa_award.pyimport math
  • def main() print This program determines
    student awards\ntry GPA float(input(Enter
    the students GPA)) if GPA gt 3.8 print
    Congratulations! You won the Mitchell
    scholarship! elif GPA gt 3.5 and GPA lt
    3.8 print Congrats! You won the Wilson
    award! elif GPA gt 3.0 and GPA lt 3.5 print
    OK, so you won the Anderson Honorary Pocket
    Protector. else print Sorry. Your GPA does
    not qualify for an award.except
    TypeError print You must enter a decimal point
    number.main()
Write a Comment
User Comments (0)
About PowerShow.com