Difference between revisions of "CSC111 Lab 6 2014"
(→Minions, Theater Play, and Functions) |
|||
(19 intermediate revisions by the same user not shown) | |||
Line 93: | Line 93: | ||
<br /> | <br /> | ||
− | * 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. | + | * 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!) | :Here is an outline of what your final program should look like (you have to figure out what to replace the ellipses with!) | ||
Line 110: | Line 111: | ||
− | singSong( "horse" ... ) | + | singSong( "horse" ... ) # add three more animals after horse... |
</source> | </source> | ||
<br /> | <br /> | ||
+ | |||
=Disney's Dwarves in Boxes= | =Disney's Dwarves in Boxes= | ||
<br /> | <br /> | ||
Line 166: | Line 168: | ||
+---+ | +---+ | ||
− | =Minions, Theater | + | =Minions, Theater Production, and Functions= |
[[Image:MinionSaysHi.jpg|100px|right]] | [[Image:MinionSaysHi.jpg|100px|right]] | ||
− | * Review the [[CSC111_Games_around_Functions| | + | * Review the [[CSC111_Games_around_Functions| 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== | ==Game 6== | ||
Line 190: | Line 192: | ||
* Call Student 3 and pass "Welcome" and "to CSC111 Lab 6!" | * Call Student 3 and pass "Welcome" and "to CSC111 Lab 6!" | ||
+ | <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= | ||
+ | <br /> | ||
+ | ==Something Different== | ||
+ | * Paste the code below in Idle. Figure out what it does before running it. Then run it. | ||
+ | <br /> | ||
+ | :<source lang="python"> | ||
+ | isGood = True | ||
+ | for i in range( 5 ): | ||
+ | print( isGood ) | ||
+ | isGood = not isGood | ||
+ | </source> | ||
+ | <br /> | ||
+ | :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 /> | ||
+ | |||
+ | ==Printing a Simple Small Chessboard== | ||
+ | <br /> | ||
+ | * Copy/paste the program below in Idle: | ||
+ | <br /> | ||
+ | ::<source lang="python"> | ||
+ | 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 ) | ||
+ | </source> | ||
+ | <br /> | ||
+ | * Run the program. | ||
+ | * Figure out how it generated the line <tt>#.#.</tt> 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 <tt>.#.#</tt> | ||
+ | <br /> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | |||
+ | ==Challenge 4== | ||
+ | |} | ||
+ | [[Image:QuestionMark4.jpg|right|120px]] | ||
+ | <br /> | ||
+ | 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. | ||
+ | <br /> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ==Challenge 5== | ||
+ | |} | ||
+ | [[Image:QuestionMark5.jpg|right|120px]] | ||
+ | <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[[CSC111 Lab 6 Solution Programs 2014|.]] | ||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | |||
+ | =Submission= | ||
+ | <br /> | ||
+ | Submit your program to this URL: [http://cs.smith.edu/~thiebaut/111b/submitL6.php | http://cs.smith.edu/~thiebaut/111b/submitL6.php ] | ||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | |||
+ | =Solution Programs= | ||
+ | <source lang="python"> | ||
+ | # 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 | ||
+ | """ | ||
+ | </source> | ||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | You may start on [[CSC111 Homework 6 2014 | Homework 6]] if you finish early! | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | [[Category:CSC111]][[Category:Python]][[Category:Labs]] |
Latest revision as of 17:44, 6 March 2014
--D. Thiebaut (talk) 15:00, 3 March 2014 (EST)
Contents
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 |
- 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 |
- 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 |
- 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
- 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 |
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 |
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!