In the following code, could someone please explain these if statements? elif 5 <= unhappiness <= 10:, & elif 11 <= unhappiness <15:, they have two arguments, one on each side which is something I haven't seen before. Also, what the one to the far right for anyway? Also, this program contains an endless loop if someone happens to find it please let me know. 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.")

hi,

elif 5 <= unhappiness <= 10

is the same thing as

elif 5 <= unhappiness and unhappiness <= 10

it's useful if you have long expressions and is evaluated to True only if the two conditions are True :)

the endless loop seems to be in the main function. I think you've got the indentation wrong. You need to ask for the input inside the while loop and all if..elif should be inside as well. Like this:

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."

Edited 7 Years Ago by masterofpuppets: n/a

This question has already been answered. Start a new discussion instead.