In the following program, how does the value of crit_name, end up in the self.name = name value? Is it because it's passed to that class and name is the only value in the __init__ parameter that has no default value? Thanks.

# Critter Caretaker
# A virtual pet to care for

class Critter(object):
    """A virtual pet"""
    
    def __init__(self, name, hunger = 0, boredom = 0):
        self.name = name
        self.hunger = hunger
        self.boredom = boredom
        
    def _pass_time(self):
        self.hunger += 1
        self.boredom += 1
        
    def _get_mood(self):
        unhappiness = self.hunger + self.boredom
        
        if unhappiness < 5:
            mood = "happy"
        elif 5 <= unhappiness <= 10:
                mood = "okay"
        elif 11 <= unhappiness <15:
                mood = "mad"
        else:
            mood = "mad"
        return mood
    
    mood = property(_get_mood)
    
    def talk(self):
        print "I'm", self.name, "and I feel", self.mood, "now.\n"
        
        self._pass_time()
        
    def eat(self, food = 4):
        print "Brruppp. Thank you."
        
        self.hunger -= food
        
        if self.hunger < 0:
            self.hunger = 0
        self._pass_time()
        
    def play(self, fun = 4):
        print "Wheee!"
        
        self.boredom -= fun
        
        if self.boredom < 0:
            self.boredom = 0
        self._pass_time()
        
def main():
    crit_name = raw_input("What do you want to name your critter?: ")
    crit = Critter(crit_name)
    
    choice = None
    
    while choice != "0":
        print \
        """
        Critter Caretaker
        
        0 - Quit
        1 - Listen to your critter
        2 - Feed your critter
        3 - play with your critter
        """
        
        choice = raw_input("Choice: ")
        print
        
        # exit
        if choice == "0":
            print "Good-bye."
            
        # listen to your critter
        elif choice == "1":
            crit.talk()
            
        # feed your critter
        elif choice =="2":
            crit.eat()
    
        # play with your critter
        elif choice == "3":
            crit.play()
            
        # some unknown choice
        else:
            print "\nSorry, but", choice, "isn't a valid choice."
        
        
main()
("\n\nPress the enter key to exit.")
class Critter(object):
    """A virtual pet"""
    
    def __init__(self, name, hunger = 0, boredom = 0):
        self.name = name
        self.hunger = hunger
        self.boredom = boredom

crit = Critter('killer')
print crit.name
'''
killer
'''

#Let look at what the class store
print crit.__dict__
'''
{'name': 'killer', 'boredom': 0, 'hunger': 0}
'''
#We only have to pass one argument(name) the two other are default value set to 0
#We can acess default value the same way as name
print crit.boredom
'''
0
'''
Member Avatar for masterofpuppets

In the following program, how does the value of crit_name, end up in the self.name = name value? Is it because it's passed to that class and name is the only value in the __init__ parameter that has no default value?

Yes, when you create the new object like
crit = Critter( crit_name )

you call the constructor for the class and assign the parameter 'name' in __init__ to be whatever the user inputs. So then self.name gets the value of 'name' and since you've initialized boredom and hunger to 0 there's no need to call the constructor like this Critter( crit_name, 0, 0 ) :)
Note that if you call Critter( crit_name, 1, 2 ) for example, the values for boredom and hunger will change to 1, 2, they will not remain zero :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.