Difference between revisions of "CSC111 Lab 6 2014"
(→Printing a Simple Small Chessboard) |
(→Something Different) |
||
Line 196: | Line 196: | ||
* Paste the code below in Idle. Figure out what it does before running it. Then run it. | * Paste the code below in Idle. Figure out what it does before running it. Then run it. | ||
<br /> | <br /> | ||
− | <source lang="python"> | + | :<source lang="python"> |
isGood = True | isGood = True | ||
for i in range( 5 ): | for i in range( 5 ): | ||
Line 205: | Line 205: | ||
: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 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 :-) | ||
<br /> | <br /> | ||
+ | |||
==Printing a Simple Small Chessboard== | ==Printing a Simple Small Chessboard== | ||
<br /> | <br /> |
Revision as of 19:30, 3 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.
- 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
- 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!"
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 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 :-)
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 its output becomes:
#.#.#.#.#.#.#.#. .#.#.#.#.#.#.#.# #.#.#.#.#.#.#.#. .#.#.#.#.#.#.#.# #.#.#.#.#.#.#.#. .#.#.#.#.#.#.#.# #.#.#.#.#.#.#.#. .#.#.#.#.#.#.#.#
- which represents an 8x8 chessboard 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.