Posted By

mladoux on 03/18/14


Tagged


Versions (?)

Dog Class


 / Published in: Python
 

Python 3 Homework

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3.  
  4. ''' Dog Class '''
  5. class Dog:
  6.  
  7. ''' Initialize dog '''
  8. def __init__(self, name):
  9. self.name = name
  10. self.alive = True
  11.  
  12. ''' Modify string output '''
  13. def __str__(self):
  14.  
  15. ''' Check if our dog is alive '''
  16. if self.alive:
  17. return "A dog named " + self.name
  18.  
  19. ''' Our dog is dead '''
  20. return "A dead dog named " + self.name
  21.  
  22. ''' Make dog bark '''
  23. def bark(self):
  24. if self.alive:
  25. ''' Our dog barks '''
  26. print(self.name + ": Bark! Bark! Bark!")
  27. return
  28.  
  29. ''' Silly, dead dogs do not bark '''
  30. print("Dead dogs don't bark!")
  31.  
  32.  
  33. ''' KILL THE DOG '''
  34. def die(self):
  35. if self.alive:
  36. ''' Our dog died TT__TT '''
  37. self.alive = False
  38. print(self.name + " died.")
  39. return
  40.  
  41. ''' You can not kill a dead dog '''
  42. print(self.name + " is already dead.")

Report this snippet  

You need to login to post a comment.