Advertisement
Guest User

Untitled

a guest
Oct 7th, 2015
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. class MissManners:
  2. """A container class that only forward messages that say please.
  3.  
  4. >>> v = VendingMachine('teaspoon', 10)
  5. >>> v.restock(2)
  6. 'Current teaspoon stock: 2'
  7.  
  8. >>> m = MissManners(v)
  9. >>> m.ask('vend')
  10. 'You must learn to say please first.'
  11. >>> m.ask('please vend')
  12. 'You must deposit $10 more.'
  13. >>> m.ask('please deposit', 20)
  14. 'Current balance: $20'
  15. >>> m.ask('now will you vend?')
  16. 'You must learn to say please first.'
  17. >>> m.ask('please hand over a teaspoon')
  18. 'Thanks for asking, but I know not how to hand over a teaspoon.'
  19. >>> m.ask('please vend')
  20. 'Here is your teaspoon and $10 change.'
  21.  
  22. >>> really_fussy = MissManners(m)
  23. >>> really_fussy.ask('deposit', 10)
  24. 'You must learn to say please first.'
  25. >>> really_fussy.ask('please deposit', 10)
  26. 'Thanks for asking, but I know not how to deposit.'
  27. >>> really_fussy.ask('please please deposit', 10)
  28. 'Thanks for asking, but I know not how to please deposit.'
  29. >>> really_fussy.ask('please ask', 'please deposit', 10)
  30. 'Current balance: $10'
  31. """
  32. "*** YOUR CODE HERE ***"
  33. def __init__(self, obj):
  34. self.object = obj
  35.  
  36. def ask (self, question, *args):
  37. if question.partition(' ')[0] == 'please':
  38. if hasattr(self.object, question.partition(' ')[2]) and not args:
  39. return getattr(self.object, question.partition(' ')[2])()
  40. elif hasattr(self.object, question.partition(' ')[2]) and args:
  41. return getattr(self.object, question.partition(' ')[2])(*args)
  42. else:
  43. return 'Thanks for asking, but I know not how to ' + str(question.partition(' ')[2]) + '.'
  44. else:
  45. return 'You must learn to say please first.'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement