Difference between revisions of "CSC111 Lab 6 2014"

From dftwiki3
Jump to: navigation, search
(Challenge 3)
 
(5 intermediate revisions by the same user not shown)
Line 193: Line 193:
 
* Call Student 3 and pass "Welcome" and "to CSC111 Lab 6!"
 
* Call Student 3 and pass "Welcome" and "to CSC111 Lab 6!"
 
<br />
 
<br />
 +
To get you started, below is the implementation of Student1 in Python:
 +
<br />
 +
<source lang="python">
 +
def student1( sentence ):
 +
    print( sentence.upper() )
 +
 +
</source>
 +
<br />
 +
 
=Chessboard=
 
=Chessboard=
 
<br />
 
<br />
Line 205: Line 214:
 
</source>
 
</source>
 
<br />
 
<br />
:We have seen how to modify integer variables inside for-loops.  We can also use boolean variable and "flip" them every loop to make them have opposite values one loop to the next.  This may come in handy at some point :-)
+
:We have seen how to modify integer variables inside for-loops.  We can also use boolean variables and "flip" them every loop to make them have opposite values one loop to the next.  This may come in handy at some point :-)
 
<br />
 
<br />
  
Line 243: Line 252:
 
[[Image:QuestionMark4.jpg|right|120px]]
 
[[Image:QuestionMark4.jpg|right|120px]]
 
<br />
 
<br />
Modify the program (including the functions) so that its output becomes:
+
Modify the program (including the functions) so that the output becomes:
  
 
  #.#.#.#.#.#.#.#.
 
  #.#.#.#.#.#.#.#.
Line 254: Line 263:
 
  .#.#.#.#.#.#.#.#
 
  .#.#.#.#.#.#.#.#
  
:which represents an 8x8 chessboard where the while cells are represented by dots and the black cells by hash-tags.
+
:which represents an 16x8 chessboard (which is the configuration that looks the most square on a video screen) where the while cells are represented by dots and the black cells by hash-tags.
 
<br />
 
<br />
 
{| style="width:100%; background:silver"
 
{| style="width:100%; background:silver"
Line 263: Line 272:
 
[[Image:QuestionMark5.jpg|right|120px]]
 
[[Image:QuestionMark5.jpg|right|120px]]
 
<br />
 
<br />
Same as Challenge 4, but make sure your program uses a for-loop to print the 8 symbols on each line, and a for-loop to print the 8 lines.
+
Same as Challenge 4, but make sure your program uses a for-loop to print the 8 symbols on each line, and a for-loop to print the 8 lines[[CSC111 Lab 6 Solution Programs 2014|.]]
  
 
<br />
 
<br />
Line 270: Line 279:
 
<br />
 
<br />
 
<br />
 
<br />
 +
 
=Submission=
 
=Submission=
 
<br />
 
<br />
Line 276: Line 286:
 
<br />
 
<br />
 
<br />
 
<br />
<!--
+
 
 
=Solution Programs=
 
=Solution Programs=
 
<source lang="python">
 
<source lang="python">
Line 418: Line 428:
 
"""
 
"""
 
</source>
 
</source>
-->
+
 
 +
<br />
 +
<br />
 +
You may start on [[CSC111 Homework 6 2014 | Homework 6]] if you finish early!
 
<br />
 
<br />
 
<br />
 
<br />

Latest revision as of 16:44, 6 March 2014

--D. Thiebaut (talk) 15:00, 3 March 2014 (EST)


Old MacDonald's Farm

Old MacDonald's farm is a song (almost) all American kids learn at one point in their life. I certainly never heard it when I grew up in France, but I think most of you will be familiar with it. If you don't know it, this YouTube video will get you acquainted with it. :-)


The goal of this problem is for you to add a section to the program below so that it prints parts of the lyrics of the famous song using a for-loop and functions.

Here's the beginning program which you have to modify:

# start with four animals in the farm
farm = [ "horse", "pig", "dog", "cat" ]

# sing(): a function that simulates singing a refrain for the song
# using the animal name.     
def sing( word ):
     print( word )

# "sing" all the names of the animals
for animal in farm:
     sing( animal )


  • Create the program in lab6.py and run it.


Challenge 1

QuestionMark1.jpg


  • Modify the function so that the output looks like the lyrics of the song:
    And on his farm he had a horse, E-I-E-I-O
    
    And on his farm he had a pig, E-I-E-I-O
    
    And on his farm he had a dog, E-I-E-I-O
    
    And on his farm he had a cat, E-I-E-I-O
  • Make sure there's a blank line between each line with an animal name.


Challenge 2

QuestionMark2.jpg


  • Modify the function some more so that the output looks like the lyrics of the song:
    Old MacDonald had a farm, E-I-E-I-O
    And on his farm he had a horse, E-I-E-I-O
    Here a horse, there a horse, everywhere a horse!
    
    Old MacDonald had a farm, E-I-E-I-O
    And on his farm he had a pig, E-I-E-I-O
    Here a pig, there a pig, everywhere a pig!
    
    Old MacDonald had a farm, E-I-E-I-O
    And on his farm he had a dog, E-I-E-I-O
    Here a dog, there a dog, everywhere a dog!
    
    Old MacDonald had a farm, E-I-E-I-O
    And on his farm he had a cat, E-I-E-I-O
    Here a cat, there a cat, everywhere a cat!



Challenge 3

QuestionMark3.jpg


  • Modify your program a third time, and introduce a new function that will receive 4 animals as parameters, and will call the function sing( ) for each one.
  • Then call your new function and give it four animals, and it should "sing" the whole song for you.
Here is an outline of what your final program should look like (you have to figure out what to replace the ellipses with!)


def sing( word ):
     print( ... )
     print( ...  word ... )
     print( ... )

def singSong( animal1, animal2, animal3, animal4 ):
     sing( animal1 )
     sing( animal2 )
     sing( animal3 )
     sing( animal4 )


singSong( "horse" ... )  # add three more animals after horse...


Disney's Dwarves in Boxes


  • We have seen before how to print the name of Disney's seven dwarves:


seven = [ "Sleepy", "Sneezy", "Bashful", "Happy", "Grumpy", "Dopey", "Doc" ]

for name in seven:
    print( name )


  • We saw in class a function that puts a box around a string:


def box( string ):
    line = "+"  +  ("-"*len( string ) )  + "+" 
    print( line )
    print( "|" + string + "|" )
    print( line )
    print()


  • Create a program that uses the code snipets above and prints the names of the 7 dwarves in boxes:
+------+
|Sleepy|
+------+

+------+
|Sneezy|
+------+

+-------+
|Bashful|
+-------+

+-----+
|Happy|
+-----+

+------+
|Grumpy|
+------+

+-----+
|Dopey|
+-----+

+---+
|Doc|
+---+

Minions, Theater Production, and Functions

MinionSaysHi.jpg
  • Review the games we played in class about directing a play with students who say sentences... This section asks you to write the code for more script.

Game 6

Here's a new game. Read it, figure out what the minions are supposed to do, and translate it into Python code.

  • Student 1 ( sentence )
    • Get sentence
    • say the sentence in UPPERCASE
  • Student 2 (sentence )
    • get sentence
    • say the sentence in lowercase
  • Student 3 (sentence1, sentence2 )
    • get sentence1 and sentence2
    • ask Student 1 to say "shhhhhhh!"
    • ask Student 1 to say sentence1
    • ask Student 2 to say setnence2
    • ask Student 2 to say "thank you"
  • Call Student 3 and pass "Welcome" and "to CSC111 Lab 6!"


To get you started, below is the implementation of Student1 in Python:

def student1( sentence ):
     print( sentence.upper() )


Chessboard


Something Different

  • Paste the code below in Idle. Figure out what it does before running it. Then run it.


 isGood = True
 for i in range( 5 ):
	print( isGood )
	isGood = not isGood


We have seen how to modify integer variables inside for-loops. We can also use boolean variables and "flip" them every loop to make them have opposite values one loop to the next. This may come in handy at some point :-)


Printing a Simple Small Chessboard


  • Copy/paste the program below in Idle:


def printBlock( visible ):
    if visible==True:
        print( "#", end="" )
    else:
        print( ".", end="" )

def printLine( visibility ):
    printBlock( visibility )
    printBlock( not visibility )
    printBlock( visibility )
    printBlock( not visibility )
    print()

printLine( True )


  • Run the program.
  • Figure out how it generated the line #.#. Make sure you understand how this happened.
  • Now change the last line of the program and replace True by False.
  • Observe the new output. Figure out how the program reverses the output to .#.#


Challenge 4

QuestionMark4.jpg


Modify the program (including the functions) so that the output becomes:

#.#.#.#.#.#.#.#.
.#.#.#.#.#.#.#.#
#.#.#.#.#.#.#.#.
.#.#.#.#.#.#.#.#
#.#.#.#.#.#.#.#.
.#.#.#.#.#.#.#.#
#.#.#.#.#.#.#.#.
.#.#.#.#.#.#.#.#
which represents an 16x8 chessboard (which is the configuration that looks the most square on a video screen) where the while cells are represented by dots and the black cells by hash-tags.


Challenge 5

QuestionMark5.jpg


Same as Challenge 4, but make sure your program uses a for-loop to print the 8 symbols on each line, and a for-loop to print the 8 lines.






Submission


Submit your program to this URL: | http://cs.smith.edu/~thiebaut/111b/submitL6.php



Solution Programs

# CSC111
# solution program for Lab 6
#

# Challenge 1
"""
# start with four animals in the farm
farm = [ "horse", "pig", "dog", "cat" ]

# sing(): a function that simulates singing a refrain for the song
# using the animal name.     
def sing( word ):
     print( "And on his farm he had a %s, E-I-E-I-O" % word )

# "sing" all the names of the animals
for animal in farm:
     sing( animal )
"""

# Challenge 2
"""
# start with four animals in the farm
farm = [ "horse", "pig", "dog", "cat" ]

# sing(): a function that simulates singing a refrain for the song
# using the animal name.     
def sing( word ):
    print( "Old MacDonald had a farm, E-I-E-I-O" )
    print( "And on his farm he had a %s, E-I-E-I-O" % word )
    print( "Here a %s, there a %s, everywhere a %s!" % ( word, word, word ) )
    print()
    
# "sing" all the names of the animals
for animal in farm:
     sing( animal )
"""

# Challenge 3
"""
# start with four animals in the farm
farm = [ "horse", "pig", "dog", "cat" ]

# sing(): a function that simulates singing a refrain for the song
# using the animal name.     
def sing( word ):
    print( "Old MacDonald had a farm, E-I-E-I-O" )
    print( "And on his farm he had a %s, E-I-E-I-O" % word )
    print( "Here a %s, there a %s, everywhere a %s!" % ( word, word, word ) )
    print()

def singSong( animal1, animal2, animal3, animal4 ):
     sing( animal1 )
     sing( animal2 )
     sing( animal3 )
     sing( animal4 )
     
# "sing" all the names of the animals
singSong( "horse", "pig", "dog", "cat" )
"""

# Disney Dwarves
"""
seven = [ "Sleepy", "Sneezy", "Bashful", "Happy", "Grumpy", "Dopey", "Doc" ]

def box( string ):
    line = "+"  +  ("-"*len( string ) )  + "+" 
    print( line )
    print( "|" + string + "|" )
    print( line )
    print()

for dwarf in seven:
    box( dwarf )
"""

# Game and Minion
def student1( sentence ):
    print( sentence.upper() )

def student2( sentence ):
    print( sentence.lower() )

def student3( s1, s2 ):
    student1( "shhhhhhhh!" )
    student1( s1 )
    student2( s2 )
    student2( "thank you" )

student3( "Welcome", "to CSC111 Lab 6!" )

# Challenge 4
"""
def printBlock( visible ):
    if visible==True:
        print( "#", end="" )
    else:
        print( ".", end="" )

def printLine( visibility ):
    printBlock( visibility )
    printBlock( not visibility )
    printBlock( visibility )
    printBlock( not visibility )
    printBlock( visibility )
    printBlock( not visibility )
    printBlock( visibility )
    printBlock( not visibility )
    print()

printLine( True )
printLine( False )
printLine( True )
printLine( False )
printLine( True )
printLine( False )
printLine( True )
printLine( False )
"""

# Challenge 5
"""
def printBlock2( visible ):
    if visible==True:
        print( "#", end="" )
    else:
        print( ".", end="" )

def printLine( visibility ):
    for i in range( 8 ):
        printBlock2( visibility )
        visibility = not visibility
    print()

firstBlack = True
for i in range( 8 ):
    printLine( firstBlack )
    firstBlack = not firstBlack 
"""



You may start on Homework 6 if you finish early!