Difference between revisions of "CSC111 Lab 3 2011 Solutions"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- <br /> <source lang="python"> # Lab3 solution programs # D. Thiebaut # single for-loops def main1(): for i in range( 3, 8 ): print( i ) print( '...")
 
 
(One intermediate revision by the same user not shown)
Line 2: Line 2:
 
----
 
----
 
<br />
 
<br />
 
+
=Solution programs for [[CSC111 Lab 3 2011| Lab #3]]=
 
<source lang="python">
 
<source lang="python">
  
Line 39: Line 39:
  
 
def main2():
 
def main2():
 +
    #  display this pattern:
 +
    #
 +
    #----
 +
    #*..-----??
 +
    #**....------????
 +
    #***......-------??????
 +
    #****........--------????????
 +
    #*****..........---------??????????
 +
    #******............----------????????????
 +
    #*******..............-----------??????????????
 +
    #********................------------????????????????
 +
    #*********..................-------------??????????????????
 
     print( '\n', '-' * 80 ,'\n' )
 
     print( '\n', '-' * 80 ,'\n' )
 
     # real challenge
 
     # real challenge
Line 50: Line 62:
  
 
def main3():
 
def main3():
 +
    # Old Macdonald's Farm
 
     print( '\n', '-' * 80 ,'\n' )
 
     print( '\n', '-' * 80 ,'\n' )
 
     farm = [ "dog", "cat", "horse" ]
 
     farm = [ "dog", "cat", "horse" ]
Line 87: Line 100:
 
     farm = [ "horse", "pig", "dog", "cat" ]
 
     farm = [ "horse", "pig", "dog", "cat" ]
 
      
 
      
     # display the names of the animals
+
     # display the lyrics with the right animal...
 
     for animal in farm:
 
     for animal in farm:
 
         #print( animal )
 
         #print( animal )
Line 98: Line 111:
  
 
     print( '\n', '-' * 80 ,'\n' )
 
     print( '\n', '-' * 80 ,'\n' )
     # start with four animals in the farm
+
     # ask the user for 4 animals...
 
     farm = []
 
     farm = []
 
     for i in range( 4 ):
 
     for i in range( 4 ):
Line 105: Line 118:
  
 
     print( "\n" * 3 )     
 
     print( "\n" * 3 )     
     # display the names of the animals
+
     # display the names of the animals inside the lyrics...
 
     for animal in farm:
 
     for animal in farm:
 
         #print( animal )
 
         #print( animal )

Latest revision as of 11:35, 22 September 2011

--D. Thiebaut 12:31, 22 September 2011 (EDT)



Solution programs for Lab #3

# Lab3 solution programs
# D. Thiebaut


# single for-loops
def main1():
    for i in range( 3, 8 ):
        print( i )

    print( '\n', '-' * 80 ,'\n' )
    # without range()
    for i in [3, 4, 5, 6, 7]:
        print( i )

    print( '\n', '-' * 80 ,'\n' )
    # only even numbers between 4 and 20 included
    for i in range( 4, 21, 2 ):
        print( i )

    print( '\n', '-' * 80 ,'\n' )
    # only odd nuumbers between -3 and 19 included
    for i in range( -3, 20, 2 ):
        print( i )

    print( '\n', '-' * 80 ,'\n' )
    # user-controlled loop
    print( "I will print a list of numbers for you." )
    start = eval( input( "Where should I start? " ) )
    stop  = eval( input( "What is the last number in the series? " ) )
    for i in range( start, stop+1, 1 ):
        print( i )

def main2():
    #  display this pattern:
    #
    #---- 
    #*..-----??
    #**....------????
    #***......-------??????
    #****........--------????????
    #*****..........---------??????????
    #******............----------????????????
    #*******..............-----------??????????????
    #********................------------????????????????
    #*********..................-------------??????????????????
    print( '\n', '-' * 80 ,'\n' )
    # real challenge
    for i in range( 0, 10 ):
        noStars  = i
        noDots   = i*2
        noDashes = 4+i
        noQMarks = noDots
        #print( noStars, noDots, noDashes, noQMarks )
        print( noStars *'*' + noDots * '.' + noDashes *'-' + noQMarks *'?' )

def main3():
    # Old Macdonald's Farm
    print( '\n', '-' * 80 ,'\n' )
    farm = [ "dog", "cat", "horse" ]
    farm.append( "pig" )
    # print the contents of the farm
    for animal in farm:
        print( animal )        
    
    print( '\n', '-' * 80 ,'\n' )
    farm = [ ]
    farm.append( "dog" )   # add a dog to it
    farm.append( "cat" )   # then a cat
    farm.append( "horse" ) # then a horse
    farm.append( "pig" )   # and a pig

    for animal in farm:
        print( animal )    

    print( '\n', '-' * 80 ,'\n' )
    farm = []
    for i in range( 4 ):
        animal = input( "Give me the name of an animal: " )
        farm.append( animal )

    for animal in farm:
        print( animal )    

    print( '\n', '-' * 80 ,'\n' )
    counter = 0
    for animal in farm:
        print( "animal #", counter+1, " : a ", animal, sep ='' )
        counter = counter + 1

def main4():
    print( '\n', '-' * 80 ,'\n' )
    # start with four animals in the farm
    farm = [ "horse", "pig", "dog", "cat" ]
    
    # display the lyrics with the right animal...
    for animal in farm:
        #print( animal )
        print( "Old MacDonald had a farm, E-I-E-I-O" )
        print( "And on his farm he had a", animal +", E-I-E-I-O" )
        print( "Here a " + animal +", there a " + animal +", everywhere a " 
                + animal + "!" )
        print()
        

    print( '\n', '-' * 80 ,'\n' )
    # ask the user for 4 animals...
    farm = []
    for i in range( 4 ):
        animal = input( "Give me the name of an animal: " )
        farm.append( animal )

    print( "\n" * 3 )    
    # display the names of the animals inside the lyrics...
    for animal in farm:
        #print( animal )
        print( "Old MacDonald had a farm, E-I-E-I-O" )
        print( "And on his farm he had a", animal +", E-I-E-I-O" )
        print( "Here a " + animal +", there a " + animal +", everywhere a " 
                + animal + "!" )
        print()
    
main1()
main2()
main3()
main4()