Difference between revisions of "CSC111 Lab 6 Solution Programs 2011"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- <source lang="python"> # lab 6 solutions programs # CSC111 from graphics import * def challenge1(): fname = input( "Your first name? " ) lname = input( "Y...")
 
 
Line 102: Line 102:
 
          
 
          
 
def main():
 
def main():
     #challenge1()
+
     challenge1()
     #challenge2()
+
     challenge2()
     #challenge3()
+
     challenge3()
     #challenge4()
+
     challenge4()
     #challenge5()
+
     challenge5()
 
     animation()
 
     animation()
 
      
 
      

Latest revision as of 16:31, 13 October 2011

--D. Thiebaut 17:29, 13 October 2011 (EDT)


 
# lab 6 solutions programs
# CSC111
from graphics import *

def challenge1():
    fname = input( "Your first name? " )
    lname = input( "Your last name?  " )
    fullName = fname.capitalize() + ' ' + lname.capitalize()
    print( "-" * 60 )
    print( fullName.center( 60 ) )
    print( "-" * 60 )

def challenge2():
    sentence = """Strength is the capacity to break a chocolate 
              bar into four pieces with your bare hands - and 
              then eat just one of the pieces"""
    oldStr = [ "chocolate", "piece", "hand", "break" ]
    newStr = [ "carrot", "chunk", "elbow", "melt"  ]

    for i in range(  len( oldStr ) ):
       sentence = sentence.replace( oldStr[i], newStr[i] )

    print( sentence )

def challenge3():
    sentence = """Strength is the capacity to break a chocolate 
              bar into four pieces with your bare hands - and 
              then eat just one of the pieces"""
    words = sentence.split()
    print( "number of words = ", len( words ) )

def challenge4():
    sentence = """Strength is the capacity to break a chocolate 
              bar into four pieces with your bare hands - and 
              then eat just one of the pieces"""
    words = sentence.split()
    print( "first word = ", words[0] )
    print( "last word  = ", words[-1] )

def challenge5():
    NYTBestSellers = """1. THE HELP, by Kathryn Stockett
                                2. WORST CASE, by James Patterson and Michael Ledwidge
                                3. THE LOST SYMBOL, by Dan Brown
                                4. POOR LITTLE BITCH GIRL, by Jackie Collins
                                5. WINTER GARDEN, by Kristin Hannah """

    # first split the strings into individual lines 
    lines = NYTBestSellers.split( '\n' )

    # process each line separately, and split it on a comma
    for line in lines:
        # split on the comma into 2 fields, the title and the author
        title, author = line.split( ',' )
        # strip both
        title = title.strip()
        author= author.strip()
        # remove "by" from the author
        author = author.replace( "by ", "" )
        # print
        print( author, ': ', title, sep='' )


def animation():
    w   = 600
    h   = 400
    win = GraphWin( "moving!", w, h )
 
    c1 = Circle( Point( 30, 30 ), 20 )
    c2 = Circle( Point( 100, 30 ), 20 )
    c1.setFill( "black" )
    c2.setFill( "black" )
    r  = Rectangle( Point( 30,30 ), Point( 100, 30-70 ) )
    r.setFill( "yellow" )
    c1.draw( win )
    c2.draw( win )
    r.draw( win )
 
    speedX = 5
    speedY = 2
    for i in range( 200 ):
        c1.move( speedX, speedY )
        c2.move( speedX, speedY )
        r.move( speedX, speedY )
        # time.sleep( 0.01 ) # wait 1/100th of a sec

    speedX = -speedX
    speedY = -speedY
    for i in range( 200 ):
        c1.move( speedX, speedY )
        c2.move( speedX, speedY )
        r.move( speedX, speedY )
    
    win.getMouse()
    win.close()

        
        
def main():
    challenge1()
    challenge2()
    challenge3()
    challenge4()
    challenge5()
    animation()
    
main()