Difference between revisions of "CSC111 Exercise Boolean Variables"
(→Evaluating boolean expressions) |
(→Evaluating boolean expressions) |
||
(One intermediate revision by the same user not shown) | |||
Line 73: | Line 73: | ||
=Mini Challenge= | =Mini Challenge= | ||
+ | [[Image:ValentinesDayNoho.jpg|right|100px]] | ||
Don't try this with your computer. Just think about it and try to figure it out yourself: | Don't try this with your computer. Just think about it and try to figure it out yourself: | ||
Line 85: | Line 86: | ||
</source> | </source> | ||
<br /> | <br /> | ||
+ | |||
=Evaluating boolean expressions= | =Evaluating boolean expressions= | ||
Line 91: | Line 93: | ||
* exp1, exp2, exp3, exp4, exp5 = True, False, False, True, True | * exp1, exp2, exp3, exp4, exp5 = True, False, False, True, True | ||
* exp1, exp2, exp3, exp4, exp5 = False, False, True, True, True | * exp1, exp2, exp3, exp4, exp5 = False, False, True, True, True | ||
+ | <br /> | ||
+ | <br /> | ||
<source lang="python"> | <source lang="python"> | ||
exp1 = ... # an expression that evaluates to True or False | exp1 = ... # an expression that evaluates to True or False |
Latest revision as of 11:38, 14 February 2014
--D. Thiebaut (talk) 09:02, 14 February 2014 (EST)
Contents
Boolean Variables
Rewrite the following Python statements taking the boolean expressions in the if-statement and replacing them with boolean variables.
Example:
x = int( input( "> " ) )
if x != 0:
print( "statement 1 " )
- would become
x = int( input( "> " ) )
isNot0 = x != 0
if isNot0:
print( "statement 1 " )
Exercise 1
x = int( input( "> " ) )
if x % 2 == 0 and x % 5 == 0:
print( "statement 1" )
Exercise 2
x = int( input( "> " ) )
if x % 2 == 0 and ( x < -10 or x > +10 ):
print( "statement 2" )
Exercise 3
x = int( input( "> " ) )
if x % 2 == 0 and ( x >= -10 and x <= +10 ):
print( "statement 3" )
Exercise 4
Solve Exercise 3 by taking the solution for Exercise 2 and making the smallest of editing
Exercise 5
Assume that we are dealing with graphics, and a rectangle is on the screen with a top-left corner having coordinates (x1, y1) and a bottom right corner having coordinates (x2, y2). We assume these variables are defined outside the code section shown below.
# get the location of the mouse in the window
x = mouseX()
y = mouseY()
if ( x < x1 ) or ( x > x2 ) or ( y < y1 ) or ( y > y2 ):
print( "statement 5" )
Exercise 6
Same setup as Exercise 5: this time we want to write an if-statement that will print "statement 6" only if the mouse is inside the rectangle.
Mini Challenge
Don't try this with your computer. Just think about it and try to figure it out yourself:
Is the code below valid? If it is, what does it print?
if True:
print( "Happy Valentine's Day!" )
else:
print( "Happy Spring Break!" )
Evaluating boolean expressions
Assume exp1, exp2, exp3, exp4, and exp5 are boolean expressions that receive the values True or False. Which statement gets printed for:
- exp1, exp2, exp3, exp4, exp5 = True, False, False, True, True
- exp1, exp2, exp3, exp4, exp5 = False, False, True, True, True
exp1 = ... # an expression that evaluates to True or False
exp2 = ... # an expression that evaluates to True or False
exp3 = ... # an expression that evaluates to True or False
exp4 = ... # an expression that evaluates to True or False
exp5 = ... # an expression that evaluates to True or False
if exp1 and ( ( exp2 or not exp3 ) or not ( exp4 and exp5 ) ):
print( "statement 1" )
else:
print( "statement 2" )
The test above is a good example of lazy evaluation!