-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattribute_critter.py
More file actions
55 lines (45 loc) · 2.38 KB
/
attribute_critter.py
File metadata and controls
55 lines (45 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Name:
# Date:
# Chapter 8 Notes: Object Oriented Programming
# attribute_critter.py
# Description: Demonstrates defining and initializing objects with attributes and the __str__ method
# An ATTRIBUTE is data that is associated with an object, sometimes called an instance variable.
# A constructor gives the programmer an opportunity to set up the initial attributes for an object.
class Critter:
"""A virtual pet"""
# The constructor is automatically invoked when a new object is instantiated and can be used to
# initialize the attributes of an object.
def __init__(self, new_name, new_mood = "happy"): # name is a parameter; still need self
"""Initializes the Critter's attributes"""
# SELF.ATTRIBUTE is the syntax for attributes
self.name = new_name #assigning the parameter value as the object's attribute
self.mood = new_mood # self.mood is the object's attribute
self.age = 0 # every Critter begins life at age 0
print("\nA new critter",self.name,"has been born and is", self.age, "years old!")
# A STRING METHOD creates a string representation of your object and returns it; typically the string will contain a description of all the attributes of an object.
# All objects should include a string method and you are required to follow the def __str__(self): method header naming scheme.
# The returned string will now display when the object is printed.
def __str__(self):
"""String representation of a Critter object"""
my_string = ""
my_string += "\nname: " + self.name
my_string += "\nmood: " + self.mood
my_string += "\nage: " + str(self.age)
return my_string
# CHALLENGE: write a method that prints the object's name, age, and mood
def talk(self):
"""Critter says its name"""
print("Hi. I'm", self.name + ", I'm", self.age, "years old,", "and I'm", self.mood + ".")
def main():
crit1 = Critter("Poochie", "happy") # only 2 arguments sent!
crit1.talk()
crit2 = Critter("Randolph", "sad")
crit2.talk()
# INSTRUCTOR'S NOTE: demonstrate below code before implementing the string method.
# use the String method by printing the object's reference variable
print("\nPrinting crit1:")
print(crit1)
print("\nPrinting crit2:")
print(crit2)
# show in http://pythontutor.com/
main()