EECS 12 Summer 2006 Homework 3

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.




Problem 0: 50 points

Modify your program from problem 2.1 so that users can add their own sounds. Call it

  1. Instead of a big if statement, use a dictionary to store the sounds.
  2. The user should type new [name] [sound] if they want to make a new sound. See interaction below for example.

Hint: You can use the split method on the user input to break it into the first part and the second part. You can then use len to see if the user included a number of times. You can do this instead of the finding and slicing that you did in homework 2.

Hint: Make sure to initialize your dictionary OUTSIDE of the loop, or it will get reset every time through the loop.

Hint: The dictionary will be reset each time you run the program, so a sound that you enter during one run will not be there during the next run.

Hint: stop and new should not be in the dictionary. They should be the only things left in the if statement, and the sounds should happen in else

Hint: Use has_key on the animal name before you try to get it to make sure that you don't get a dictionary error.

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

effect: cow
effect: horse
effect: dog
effect: cat
effect: horse 5
Neigh! Neigh! Neigh! Neigh! Neigh!
effect: turtle
I don't know that effect!
effect: new turtle Crawl!
New effect turtle added
effect: turtle 3
Crawl! Crawl! Crawl!
effect: stop

Problem 1: 50 points

Create a program called that deals hands of cards.

Hint: You can make an empty list like this: deck = []

Hint: You can use the append method of a list to add new items to the end

Hint: Try to get it to work without shuffling first. It will be easier to tell if you are missing cards.

Here is a sample session:

Number of jokers? 0
Number of hands? 2
Number of cards in a hand? 4
*** Hand 1 ***
Seven of Clubs
Four of Diamonds
Five of Spades
Ace of Clubs
*** Hand 2 ***
Ten of Spades
Eight of Clubs
Ace of Diamonds
Nine of Spades
Deal again (y/n)? y
Number of jokers? 0
Number of hands? 10
Number of cards in a hand? 6
There will not be enough cards!
Number of jokers? 10
Number of hands? 3
Number of cards in a hand? 4
*** Hand 1 ***
King of Clubs
Five of Diamonds
Three of Spades
*** Hand 2 ***
Ace of Hearts
Eight of Spades
Four of Spades
*** Hand 3 ***
Four of Diamonds
Ten of Hearts
Six of Clubs
Deal again (y/n)? n