Difference between revisions of "CSC111 Lab 6 2014"

From dftwiki3
Jump to: navigation, search
(Minions, Theater Play, and Functions)
(Minions, Theater Production, and Functions)
Line 168: Line 168:
 
=Minions, Theater Production, and Functions=
 
=Minions, Theater Production, and Functions=
 
[[Image:MinionSaysHi.jpg|100px|right]]
 
[[Image:MinionSaysHi.jpg|100px|right]]
* Review the [[CSC111_Games_around_Functions| game]] we played in class about directing a play with students who say sentences...  This section asks you to write the code for more scripts.
+
* 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 scripts.
  
 
==Game 6==
 
==Game 6==

Revision as of 16:31, 3 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.
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" ... )


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 scripts.

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!"