CSC111 Exercises with If-Statements

From dftwiki3
Revision as of 08:26, 20 October 2011 by Thiebaut (talk | contribs) (Exercises)
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 )

main1()
main2()

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