CSC111 Exercises with If-Statements

From dftwiki3
Revision as of 17:58, 22 October 2011 by Thiebaut (talk | contribs) (Solution 3)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--D. Thiebaut 09:23, 20 October 2011 (EDT)




Exercises

You can use this program to test your answers to the questions:

from graphics import *
import time

def main1():
    w = 500
    h = 500
    radius = 20
    win = GraphWin( "lab 7", w, h )

    c = Circle( Point( w/2, h/2 ), radius )
    c.setFill( "magenta" )
    c.draw( win )

    dirX = 5  # speed in the horizontal direction

    for step in range( 1000 ):
        # uncomment next line if ball moves too fast
        # time.sleep( 0.05 )  # 5/100th sec.
        c.move( dirX, 1 )        
       

    Text( Point( w//2, h//2 ), "Click me to quit!" ).draw( win )
    win.getMouse()
    win.close()

def main2():
     L = [ 1, 10, -3, 30, -10, -10, -10, 2, 55 ]
     print( "L = ", L )

def main3():
     x = eval( input( "> " ) )
     y = eval( input( "> " ) )
     print( x, '/', y, '=', x/y )

#main1()
#main2()
main3()

Exericse 1

  • Program asks for 2 numbers from user, then show the result of dividing first by second.



x = eval( input( "> " ) )
y = eval( input( "> " ) )
print( x, '/', y, '=', x/y )



  • Make this program robust!

Exercise 2

  • User is asked a question and must respond by Yes or by No, using the letter y or n only. Make the program detect correctly the user input.

Exercise 3

L = [ 1, 10, -3, 30, -10, -10, -10, 2, 55 ]
  • Given the list above, write the code necessary to remove the numbers that are strictly less than 0 from L
  • Same question, but we want the list to contain only the positive numbers that are multiples of 5.


Exercise 4

  • Write the code for a graphics program that will make a circle move from one end of the window to the other, and such that the circle will be yellow in the left half of the window, and will turn red when it is in the right half.


Exercise 5

  • Make the ball bounce off the right vertical side of the window










Solutions

Solution 1

x = eval( input( "> " ) )
y = eval( input( "> " ) )
if y==0:
      print( "Invalid input.  Cannot divide by 0!" )
else:
      print( x, '/', y, '=', x/y )

Solution 2

 # this version only checks for 'y' or 'Y'.  If the user types something else
 # then the program will treat the answer as "NO"
 ans = input( "Do you like chocolate (Y/N)? " )
 if ans.lower()=='y':
      print( "You said yes!" )
 else:
      print( "Sorry you don't like it!" )
 
 # this version checks for both Yes and for No answers.
 ans = input( "Do you like chocolate (Y/N)? " )
 if ans.lower()=='y':
      print( "You said yes!" )
 else:
      if ans.lower() =='n':
           print( "Sorry you don't like it!" )
      else:
           print( "Invalid input!" )

Solution 3

 L = [ 1, 10, -3, 30, -10, -10, -10, 2, 55 ]
 L2 = []
 for x in L:
     if x >= 0:
         L2.append( x )
 L = L2
 print( "List with positive numbers = ", L )
 
 L = [ 1, 10, -3, 30, -10, -10, -10, 2, 55 ]
 L2 = []
 for x in L:
     if x >=0 and x % 5 == 0:
          L2.append( x )
 L = L2
 print( "Positive numbers that are multiple of 5: ", L )

Solution 4

from graphics import *
import time
 
def main1():
    w = 500
    h = 500
    radius = 20
    win = GraphWin( "lab 7", w, h )
 
    c = Circle( Point( w/2, h/2 ), radius )
    c.setFill( "magenta" )
    c.draw( win )
 
    dirX = 5  # speed in the horizontal direction
    dirY  = 3
 
    for step in range( 1000 ):
        # uncomment next line if ball moves too fast
        # time.sleep( 0.05 )  # 5/100th sec.
        c.move( dirX, dirY )        
 
        # test to see if the ball is outside the window
        if c.getCenter().getX() < 0 or c.getCenter().getX() > w:
              dirX = -dirX
        if c.getCenter().getY() < 0 or c.getCenter().getY() > h:
              dirY = -dirY     
 
    Text( Point( w//2, h//2 ), "Click me to quit!" ).draw( win )
    win.getMouse()
    win.close()
 
def main2():
     L = [ 1, 10, -3, 30, -10, -10, -10, 2, 55 ]
     print( "L = ", L )
 
def main3():
     x = eval( input( "> " ) )
     y = eval( input( "> " ) )
     print( x, '/', y, '=', x/y )
 
#main1()
#main2()
main3()