Difference between revisions of "CSC111 Lab7 Solution Programs 2011"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- <onlydft> <source lang="python"> from graphics import * import time def ifStatement1(): print( "Please enter 2 numbers:" ) x = eval( input( "> " ) ) y = e...")
 
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
--[[User:Thiebaut|D. Thiebaut]] 13:58, 19 October 2011 (EDT)
 
--[[User:Thiebaut|D. Thiebaut]] 13:58, 19 October 2011 (EDT)
 
----
 
----
<onlydft>
+
 
<source lang="python">
 
<source lang="python">
  
Line 9: Line 9:
  
 
def ifStatement1():
 
def ifStatement1():
 +
  """contains several if statements"""
 +
 +
  # test for zero divisor
 
   print( "Please enter 2 numbers:" )
 
   print( "Please enter 2 numbers:" )
 
   x = eval( input( "> " ) )
 
   x = eval( input( "> " ) )
Line 40: Line 43:
  
 
def ball():
 
def ball():
 +
    """moves a ball around the graphics window and make it bounce off
 +
    the sides.  The ball changes color as it crosses the middle of the graphics
 +
    window (horizontally), and bounces off the walls when its side hits the walls.
 +
    """
 +
 
     w = 400
 
     w = 400
 
     h = 400
 
     h = 400
Line 90: Line 98:
 
     print( "!" ) # happy birthday to you!
 
     print( "!" ) # happy birthday to you!
  
   
+
def main():
#ifStatement1()
+
  ifStatement1()
#ball()
+
 
singSong( "Frida" )
+
  ball()
for ch in "abcdefghijklmnopqrstuvwxyz":
+
 
    singSong( "111a-a" + ch )
+
  singSong( "Frida" )
    print()
+
 
 +
  for ch in "abcdefghijklmnopqrstuvwxyz":
 +
      singSong( "111a-a" + ch )
 +
      print()
 
      
 
      
  
 
</source>
 
</source>
</onlydft>
+
 
<br />
 
<br />
  

Latest revision as of 17:03, 20 October 2011

--D. Thiebaut 13:58, 19 October 2011 (EDT)


from graphics import *
import time

def ifStatement1():
   """contains several if statements"""

   # test for zero divisor 
   print( "Please enter 2 numbers:" )
   x = eval( input( "> " ) )
   y = eval( input( "> " ) )
 
   if ( y != 0 ):
      print( x, "/", y, "=", x/y )
   else:
      print( "Illegal value for y: division by 0 is not allowed or defined" )

   #--- find smallest/largest of 2 numbers ---
   print( "Please enter 2 numbers:" )
   x = eval( input( "> " ) )
   y = eval( input( "> " ) )

   if x < y:
       print( x, "is the smallest of the two,", y, "the largest" )
   else:
       print( y, "is the smallest of the two,", x, "the largest" )
        

   #--- find smallest/largest of two strings---
   print( "Please enter 2 words:" )
   x = input( "> " )
   y = input( "> " )

   if x.lower() < y.lower():
       print( x, "is before", y, "in alphabetical order" )
   else:
       print( y, "is before", x, "in alphabetical order" )

def ball():
    """moves a ball around the graphics window and make it bounce off
    the sides.  The ball changes color as it crosses the middle of the graphics
    window (horizontally), and bounces off the walls when its side hits the walls.
    """

    w = 400
    h = 400
    win = GraphWin( "lab 7", w, h )

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

    
    dirX = 3  # speed in the horizontal direction
    dirY = 2
    for step in range( 1000 ):
        c.move( dirX, dirY )        
        if not ( radius < c.getCenter().getX() < w-radius ):
            dirX = -dirX
        if not ( radius < c.getCenter().getY() < h-radius ):
            dirY = -dirY
        if c.getCenter().getX() < w//2:
            c.setFill( "purple" )
        else:
            c.setFill( "darkgreen" )
            
    
    Text( Point( w//2, h//2 ), "Click me to quit!" ).draw( win )
    win.getMouse()
    win.close()

def hbday():
    """prints "Happy birthday to you" then leaves the cursor
    at the end of the line."""
    print( "Happy birthday to you", end="" )

def dear( name ):
    """prints "Dear " followed by the name that is passed.
    Goes to the next line when done"""
    print( "Dear ", name, ",", sep="" )

def singSong( name ):
    """prints the whole birthday song, including the name passed
    """
    for i in range( 3 ):
        hbday()
        print( "," ) # happy birthday to you,

    dear( name ) # Dear xxxx,
    hbday()
    
    print( "!" ) # happy birthday to you!

def main():
   ifStatement1()

   ball()

   singSong( "Frida" )

   for ch in "abcdefghijklmnopqrstuvwxyz":
       singSong( "111a-a" + ch )
       print()