Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Decision Structures and Boolean Variables

Similar presentations


Presentation on theme: "Introduction to Decision Structures and Boolean Variables"— Presentation transcript:

1 Introduction to Decision Structures and Boolean Variables

2 Warm-Up Write a program that asks the user for their hourly rate of pay. Then, ask the user how many hours they worked this week and calculate and print their wages for the week. Format accordingly

3 Sequence Structures Thus far, we’ve been programming “sequence structures” That just means that the program will execute the statements in the order in which they appear in the source code We would now like for programs to deviate from the linear structure to adapt according to conditions being met, or not met

4 The Problem If you remember, when we were calculating the car payments, we left an option at the bottom of the code for if the price they were given was too high. The problem was, it wasn’t really an option … it was more like a forced secondary deal So, how do we get Python to make decisions …?

5 The Selection Statement
Allows program to “ask a question” and respond accordingly Simplest form: perform an action only if a certain condition is met If the condition is not met, then the action is not performed (we will learn how to accommodate for non-met conditions with a separate action later on)

6 The Selection Statement
In this program, we start by asking a question, “is it cold outside?” If the answer is yes (aka “True”) then we execute an alternate set of commands Otherwise, we continue with the program as-is

7 The Selection Statement
In Python, we use the keyword “if” to start a selection statement We must also use colons to end the selection statement The block of code reserved to execute if and only if the condition is met, must be indented within the selection statement (nested structure)

8

9 Writing a condition Python doesn’t read the way you would, so you can’t just write your conditions as questions like, “Is it cold outside?” The key is to ask a question and then compare the returned value (the answer to your question) to another value This comparison is our condition Each of these conditions must hold either “True” or “False”

10 Boolean Expressions

11 Example answer = input(“Is it cold outside?”) if answer == “yes” : # this will only execute IF and only IF the condition holds TRUE print(“Go put on a jacket!”) # then the program will return to it’s original line of code print(“Go outside.”)

12 Boolean Expressions Writing a condition

13 Boolean Expressions Named after George Boole, a 19th century English philosopher and mathematician Boole developed a system of mathematics that allows us to work with the concepts of “True” or “False” Boole is considered one of the founders of modern computer science, as his work is reflected in the way computers process binary data

14 Boolean Expressions Boolean expressions can be used as the condition of an “if” selection statement They are generally formed using relational operators which allow you to test whether a specific relationship exists between two (or more) values

15 Relational Operators A > B # A is greater than B
A < B # A is less than B A == B # A is equal to B A >= B # A is greater than OR equal to B A <= B # A is less than OR equal to B

16 Writing a Boolean Expression
charizard = 10 pikachu = 7 if charizard > pikachu: # charizard > pikachu print( “It seems that your # 10 > 7 Pikachu is no match for the # True, condition met almighty Charizard” )

17 Practice # given these variables # evaluate A = 99 A > B B = 7 B < C C = -5 B >= C D = 92 C <= D A == B + D D >= A - C C != B

18 Practice # given these variables # evaluate A = 99 A > B True B = 7 B < C C = -5 B >= C D = 92 C <= D A == B + D D >= A - C C != B

19 Practice # given these variables # evaluate A = 99 A > B True B = 7 B < C False C = -5 B >= C D = 92 C <= D A == B + D D >= A - C C != B

20 Practice # given these variables # evaluate A = 99 A > B True B = 7 B < C False C = -5 B >= C True D = 92 C <= D A == B + D D >= A - C C != B

21 Practice # given these variables # evaluate A = 99 A > B True B = 7 B < C False C = -5 B >= C True D = 92 C <= D True A == B + D D >= A - C C != B

22 Practice # given these variables # evaluate A = 99 A > B True B = 7 B < C False C = -5 B >= C True D = 92 C <= D True A == B + D True D >= A - C C != B

23 Practice # given these variables # evaluate A = 99 A > B True B = 7 B < C False C = -5 B >= C True D = 92 C <= D True A == B + D True D >= A - C False C != B

24 Practice # given these variables # evaluate A = 99 A > B True B = 7 B < C False C = -5 B >= C True D = 92 C <= D True A == B + D True D >= A - C False C != B True

25 More Boolean Operators
Don’t confuse “==“ with “=“: “=“ is used to assign variables “==“ is used to test if two values are equivalent We use “!=“ to test if two values are different (“not equal to”) “<=“ and “>=“ test for more than one relationship at a time

26 Challenge: Guess the Magic Number
Write a program that sets a magic number, anywhere from 1-10. Then, ask the user to guess a number. If they guess correctly, then print out “You guessed correctly, the number was __!” If not, tell them, “Sorry, the magic number was __.”


Download ppt "Introduction to Decision Structures and Boolean Variables"

Similar presentations


Ads by Google