04 Slide
04 Slide
b = (1 > 2)
AdditionQuiz Run
False False
boolean-expression radius >= 0?
True True
(a) (b)
SimpleIfDemo Run
1 3 5 7 2 3 6 7 4 5 6 7 8 9 10 11 16 17 18 19
9 11 13 15 10 11 14 15 12 13 14 15 12 13 14 15 20 21 22 23
17 19 21 23 18 19 22 23 20 21 22 23 24 25 26 27 24 25 26 27
25 27 29 31 26 27 30 31 28 29 30 31 28 29 30 31 28 29 30 31
Set1 Set2 Set3 Set4 Set5
GuessBirthday Run
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved.
10
Mathematics Basis for the Game
19 is 10011 in binary. 7 is 111 in binary. 23 is 11101 in binary
10000
10000 00110 1000
10 10 100
+ 1 + 1 + 1
10011 00111 11101
19 7 23
= 19
1 3 5 7 2 3 6 7 4 5 6 7 8 9 10 11 16 17 18 19
9 11 13 15 10 11 14 15 12 13 14 15 12 13 14 15 20 21 22 23
17 19 21 23 18 19 22 23 20 21 22 23 24 25 26 27 24 25 26 27
25 27 29 31 26 27 30 31 28 29 30 31 28 29 30 31 28 29 30 31
Set1 Set2 Set3 Set4 Set5
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved.
11
The Two-way if Statement
if boolean-expression:
statement(s)-for-the-true-case
else:
statement(s)-for-the-false-case
True False
boolean-expression
Statement(s) for the true case Statement(s) for the false case
(a) (b)
False
score >= 90
False
True score >= 80
False
grade = 'A' True score >= 70
False
grade = 'B' True score >= 60
grade = 'D'
grade = 'F'
pig
0: monkey
rat
1: rooster
dog ox 2: dog
3: pig
rooster tiger 4: rat
year % 12 = 5: ox
monkey rabbit 6: tiger
7: rabbit ChineseZodiac
8: dragon
sheep dragon
9: snake
horse snake 10: horse
11: sheep
Run
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved.
22
Common Errors
Most common errors in selection statements
are caused by incorrect indentation. Consider
the following code in (a) and (b).
radius = -20 radius = -20
BMI Interpretation
ComputeTax Run
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved.
27
Logical Operators
Operator Description
True False not (age > 18) is False, because (age > 18) is True.
False True not (gender == 'M') is True, because (grade == 'M') is False.
False False False (age > 18) and (gender == 'F') is True, because (age
False True False > 18) and (gender == 'F') are both True.
True False False (age > 18) and (gender != 'F') is False, because
(gender != 'F') is False.
True True True
False False False (age > 34) or (gender == 'F') is true, because (gender
False True True == 'F') is True.
True False True (age > 34) or (gender == 'M') is False, because (age >
True True True 34) and (gender == 'M') are both Talse.
TestBooleanOperators Run
LeapYear Run
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved.
33
Problem: Lottery
Write a program that randomly generates a lottery of a two-
digit number, prompts the user to enter a two-digit number,
and determines whether the user wins according to the
following rule:
• If the user input matches the lottery in exact order, the
award is $10,000.
• If the user input matches the lottery, the award is
$3,000.
• If one digit in the user input matches a digit in the
lottery, the award is $1,000.
Lottery Run
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved.
34
Conditional Operator
if x > 0:
y=1
else:
y = -1
is equivalent to
y = 1 if x > 0 else -1
(x2, y2)
radius
(x1, y1)
PointInCircle Run
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved.
40