Write a Coin class that will enable this code to run,
# Your class goes here...
if __name__ == '__main__':
coin = Coin()
print 'Your first coin is a %s.' % (coin)
purse = [coin]
print 'Adding four more coins to your purse...'
for i in range(4):
coin = Coin(random.choice([1,5,10,25,100,200]))
purse.append(coin)
print 'In your purse you now have:'
for coin in purse:
print '\ta', coin
total = 0
for coin in purse:
total += coin.value
print 'The total value of the coins in your purse is', total, 'cents.'
print 'Flipping your coins you get:',
for coin in purse:
print coin.flip(),
and produce this output,
>>>
Your first coin is a Penny.
Adding four more coins to your purse...
In your purse you now have:
a Penny
a Loonie
a Penny
a Dime
a Quarter
The total value of the coins in your purse is 137 cents.
Flipping your coins you get: Tails Tails Tails Heads Tails
>>>
Of course due to their random selection the exact coins that end up in the purse will vary from run to run, though the first coin created in the test above should always be a Penny, i.e. the default Coin to create is a Penny.
Extend the code in playing_cards_4.py
so
we can find out if a five card poker hand is a flush, i.e. all cards are from the same suit. Here's the specification code to add to the end of the module,
if chris.is_flush():
print 'That is a flush!'
else:
print 'Sorry, no flush.'
Sometimes during the course of a card game we might want or need to
verify that the deck is not corrupted, for example that it does not have
any extra Aces in it. Extend the code in playing_cards_4.py so we can check that a deck is fair.
Here's the specification code to add to the end of the module,
if d.is_fair():
print 'The deck is fair.'
else:
print 'Uh oh, this deck is unfair.'
Include the tests you used to determine that your method was working correctly in the code you submit.
Many OO programmers would look at the playing card classes developed in this module and shout,
"Wait a minute! Actual physical playing cards have two attributes, their suit and their face value. That means your
Cardobjects should have two attributes too that match the physical attributes, not this sketchy card number thing that encodes the values of the actual attributes in an opaque way."
And you know, they
might be right. Modify the module playing_cards_4.py to use
Card objects with a pair of attributes. Name the attributes
suit and face_value.
Hint1: You shouldn't change anything
after the if __name__ == '__main__': statement. Remember
that one of the goals of encapsulation is to be able drop in a new class
implementation for an older one without having to rewrite any code that
uses it.
Hint2: You don't have to rewrite that much of the
code above the if either. Think carefully about what needs
to change.
Use the following naming scheme for your program files: aassignment#pproblem#vversion#.py . So your first attempt at problem 1 on this assignment will be named a9p1v1.py and your second attempt (should there be one) will
be named a9p1v2.py .
Submit your assignment by emailing your python programs to me. I advise to submit early and submit often, since I will often review your programs as they are submitted and provide feedback that will let you improve them.