Difference between revisions of "CSC111 Exercise Boolean Variables"
(Created page with "--~~~~ ---- =Boolean Variables= Rewrite the following Python statements taking the boolean expressions in the '''if-statement''' and replacing them with boolean variables. E...") |
|||
Line 39: | Line 39: | ||
x = int( input( "> " ) ) | x = int( input( "> " ) ) | ||
− | if x % 2 == 0 and x | + | if x % 2 == 0 and ( x < -10 or x > +10 ): |
− | print( "statement | + | print( "statement 2" ) |
</source> | </source> | ||
<br /> | <br /> | ||
Line 48: | Line 48: | ||
x = int( input( "> " ) ) | x = int( input( "> " ) ) | ||
− | if x % 2 == 0 and x | + | if x % 2 == 0 and ( x >= -10 and x <= +10 ): |
− | print( "statement | + | print( "statement 3" ) |
</source> | </source> | ||
<br /> | <br /> | ||
==Exercise 4== | ==Exercise 4== | ||
<br /> | <br /> | ||
− | + | Solve Exercise 3 by taking the solution for Exercise 2 and making the smallest of editing | |
− | |||
− | |||
− | |||
− | |||
− | |||
<br /> | <br /> | ||
==Exercise 5== | ==Exercise 5== | ||
<br /> | <br /> | ||
+ | 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. | ||
<source lang="python"> | <source lang="python"> | ||
− | x = | + | # get the location of the mouse in the window |
+ | x = mouseX() | ||
+ | y = mouseY() | ||
− | if x | + | if ( x < x1 ) or ( x > x2 ) or ( y < y1 ) or ( y > y2 ): |
− | print( "statement | + | print( "statement 5" ) |
</source> | </source> | ||
<br /> | <br /> | ||
==Exercise 6== | ==Exercise 6== | ||
<br /> | <br /> | ||
− | + | 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? | ||
− | |||
− | |||
− | |||
− | |||
− | |||
<br /> | <br /> | ||
<source lang="python"> | <source lang="python"> | ||
− | + | if True: | |
− | + | print( "Happy Valentine's Day!" ) | |
− | + | else: | |
− | + | print( "Happy Spring Break!" ) | |
</source> | </source> | ||
<br /> | <br /> | ||
− | == | + | =Evaluating boolean expressions= |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<br /> | <br /> | ||
+ | Assume exp1, exp2, exp3, exp4, and exp5 are boolean expressions that receive | ||
<source lang="python"> | <source lang="python"> | ||
− | |||
− | |||
− | |||
</source> | </source> | ||
− | |||
<br /> | <br /> | ||
<br /> | <br /> |
Revision as of 10:14, 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