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.
Modify your program from problem 2.1 so
that users can add their own sounds. Call it
ifstatement, use a dictionary to store the sounds.
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.
new should not be in the
dictionary. They should be the only things left in the
statement, and the sounds should happen in else
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 Moo! effect: horse Neigh! effect: dog Woof! effect: cat Meow! 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 Goodbye!
Create a program called
cards.py that deals hands of
imports, define two lists:
suits = ['Spades', 'Clubs', 'Diamonds', 'Hearts']
values = ['Two', 'Three', 'Four', 'Five', 'Six'] values = values + ['Seven', 'Eight', 'Nine', 'Ten'] values = values + ['Jack', 'Queen', 'King', 'Ace']
generateDeck(numJokers)that returns a list of all possible cards, represented as tuples. Use nested for loops to create the list. The first part of the list should look like this:
[('Two', 'Spades'), ('Three', 'Spades'),. At the end of the list, there should be
numJokersjoker cards, each represented by the tuple
(None, 'Joker'); there should be able to be 0 jokers.
shufflefunction from the
randommodule to shuffle the deck after it is generated. Be careful:
shufflechanges the list in place and returns
dealHand(deck, size)that takes a list of cards and returns two things: a hand with size cards from the first
sizecards in deck and the rest of the deck.
printCard(card)that prints out a card like this:
Ace of Spades
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 *** Joker King of Clubs Five of Diamonds Three of Spades ************** *** Hand 2 *** Ace of Hearts Eight of Spades Joker Four of Spades ************** *** Hand 3 *** Four of Diamonds Ten of Hearts Six of Clubs Joker ************** Deal again (y/n)? n