EECS 12 Summer 2006 Homework 2

General Instructions

Please place your programs in seperate files named as described in the problem. Improperly named programs will not count. Turn in your files by placing them in the dropbox. Please be mindful of the late policy.

Grading

Correct80%
Commented10%
Attempted10%

General Notice

Sometimes you will accidentally make python go into an infinte loop... that is, a loop that will never stop on its own. In most environments, you can press Ctrl+C to break out of a loop

Problems

Problem 0: 50 points

Create a python program named commaSepLines.py that takes user input and puts everything after a comma on a new line. Do not print the commas.

Example:

input? abc,de, fgh,ijklmn opw wer qwe , efh
abc
de
 fgh
ijklmn opw wer qwe 
 efh

Use the string.find function, a while loop and slicing.

Hint: You probably want to use a variable to keep track of where you are in the user's string. Initialize the variable to zero outside of the while loop and then reassign its value inside the loop when you find a comma. Use the variable as the start argument when you call string.find.

Problem 1: 50 points

Radio stations have special equipment or computer programs that allow them to play sound effects "on demand." We are going to write a program that does the same thing, but with text instead of sound. It might not be as exciting, but we can use our imaginations, right?

Create a python program named textFX.py that does the following:

  1. Supports the FX: cow horse dog cat
  2. Allows annoying DJs to play the effect multiple times by adding a number on the end.
  3. Keeps asking for new FX until the DJ enters 'stop'
  4. If the DJ enters an effect that is not supported, it prints the message "I don't know that effect!"

Hint: BE SURE TO READ THE HINTS BELOW!

Here is a sample set of interactions. User text is in red

effect: cow 
Moo!
effect: horse 
Neigh!
effect: dog 
Woof!
effect: cat 
Meow!
effect: horse 5 
Neigh! Neigh! Neigh! Neigh! Neigh!
effect: dog 10 
Woof! Woof! Woof! Woof! Woof! Woof! Woof! Woof! Woof! Woof!
effect: turtle 
I don't know that effect!
effect: stop 
Goodbye!

Hint: Create a boolean varible doLoop = True and put your loop code inside of a while doLoop: loop. When your program should exit (when the DJ types stop), set doLoop = False to stop the loop.

Hint: Write a function that will print an animal sound a given number of times. For example, def animalSound(sound, times). You should be able to use it for both single sounds and multiple sounds.

Hint: The print command usually starts a new line each time it prints. You can avoid this by using a comma at the end:


>>> for ii in range(3):
      print 'hello'
      
hello
hello
hello
>>> for ii in range(3):
      print 'hello',
      
hello hello hello

Hint: You can print the empty string (print '') to start a new line.

Hint: Do not try to test for all of the numbers seperately. Instead, look for the space after the animal name and convert the remaining string into an integer and pass to the animal function.