Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MissManners:
- """A container class that only forward messages that say please.
- >>> v = VendingMachine('teaspoon', 10)
- >>> v.restock(2)
- 'Current teaspoon stock: 2'
- >>> m = MissManners(v)
- >>> m.ask('vend')
- 'You must learn to say please first.'
- >>> m.ask('please vend')
- 'You must deposit $10 more.'
- >>> m.ask('please deposit', 20)
- 'Current balance: $20'
- >>> m.ask('now will you vend?')
- 'You must learn to say please first.'
- >>> m.ask('please hand over a teaspoon')
- 'Thanks for asking, but I know not how to hand over a teaspoon.'
- >>> m.ask('please vend')
- 'Here is your teaspoon and $10 change.'
- >>> really_fussy = MissManners(m)
- >>> really_fussy.ask('deposit', 10)
- 'You must learn to say please first.'
- >>> really_fussy.ask('please deposit', 10)
- 'Thanks for asking, but I know not how to deposit.'
- >>> really_fussy.ask('please please deposit', 10)
- 'Thanks for asking, but I know not how to please deposit.'
- >>> really_fussy.ask('please ask', 'please deposit', 10)
- 'Current balance: $10'
- """
- "*** YOUR CODE HERE ***"
- def __init__(self, obj):
- self.object = obj
- def ask (self, question, *args):
- if question.partition(' ')[0] == 'please':
- if hasattr(self.object, question.partition(' ')[2]) and not args:
- return getattr(self.object, question.partition(' ')[2])()
- elif hasattr(self.object, question.partition(' ')[2]) and args:
- return getattr(self.object, question.partition(' ')[2])(*args)
- else:
- return 'Thanks for asking, but I know not how to ' + str(question.partition(' ')[2]) + '.'
- else:
- return 'You must learn to say please first.'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement