Difference between revisions of "CSC111 Exercise Boolean Variables"

From dftwiki3
Jump to: navigation, search
(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...")
 
(Evaluating boolean expressions)
 
(3 intermediate revisions by the same user not shown)
Line 39: Line 39:
 
   x = int( input( "> " ) )
 
   x = int( input( "> " ) )
  
   if x % 2 == 0 and x % 5 == 0:
+
   if x % 2 == 0 and ( x < -10 or x > +10 ):
         print( "statement 1" )
+
         print( "statement 2" )
 
</source>
 
</source>
 
<br />
 
<br />
Line 48: Line 48:
 
   x = int( input( "> " ) )
 
   x = int( input( "> " ) )
  
   if x % 2 == 0 and x % 5 == 0:
+
   if x % 2 == 0 and ( x >= -10 and x <= +10 ):
         print( "statement 1" )
+
         print( "statement 3" )
 
</source>
 
</source>
 
<br />
 
<br />
 
==Exercise 4==
 
==Exercise 4==
 
<br />
 
<br />
<source lang="python">
+
Solve Exercise 3 by taking the solution for Exercise 2 and making the smallest of editing
  x = int( input( "> " ) )
 
 
 
  if x % 2 == 0 and x % 5 == 0:
 
        print( "statement 1" )
 
</source>
 
 
<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 = int( input( "> " ) )
+
  # get the location of the mouse in the window
 +
   x = mouseX()
 +
  y = mouseY()  
  
   if x % 2 == 0 and x % 5 == 0:
+
   if ( x < x1 ) or ( x > x2 ) or ( y < y1 ) or ( y > y2 ):
         print( "statement 1" )
+
         print( "statement 5" )
 
</source>
 
</source>
 
<br />
 
<br />
 
==Exercise 6==
 
==Exercise 6==
 
<br />
 
<br />
<source lang="python">
+
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.
  x = int( input( "> " ) )
+
 
 +
=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:
 +
 
 +
Is the code below valid?  If it is, what does it print?
  
  if x % 2 == 0 and x % 5 == 0:
 
        print( "statement 1" )
 
</source>
 
<br />
 
==Exercise 7==
 
 
<br />
 
<br />
 
<source lang="python">
 
<source lang="python">
   x = int( input( "> " ) )
+
if True:
 
+
   print( "Happy Valentine's Day!" )
   if x % 2 == 0 and x % 5 == 0:
+
else:
        print( "statement 1" )
+
   print( "Happy Spring Break!" )
 
</source>
 
</source>
 
<br />
 
<br />
==Exercise 8==
+
 
 +
=Evaluating boolean expressions=
 +
 
 
<br />
 
<br />
<source lang="python">
+
Assume exp1, exp2, exp3, exp4, and exp5 are boolean expressions that receive the values True or False.  Which statement gets printed for:
  x = int( input( "> " ) )
+
* exp1, exp2, exp3, exp4, exp5 = True, False, False, True, True
 
+
* exp1, exp2, exp3, exp4, exp5 = False, False, True, True, True
  if x % 2 == 0 and x % 5 == 0:
 
        print( "statement 1" )
 
</source>
 
 
<br />
 
<br />
==Exercise 9==
 
 
<br />
 
<br />
 
<source lang="python">
 
<source lang="python">
   x = int( input( "> " ) )
+
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" )
  
  if x % 2 == 0 and x % 5 == 0:
 
        print( "statement 1" )
 
 
</source>
 
</source>
  
 +
<br />
 +
The test above is a good example of ''lazy evaluation''!
 
<br />
 
<br />
 
<br />
 
<br />

Latest revision as of 11:38, 14 February 2014

--D. Thiebaut (talk) 09:02, 14 February 2014 (EST)


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

ValentinesDayNoho.jpg

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!