2 Replies - 1449 Views - Last Post: 25 June 2013 - 09:34 PM Rate Topic: -----

#1 pandeee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 24-June 13

Critter farm!

Posted 24 June 2013 - 05:24 PM

Hello folks, I have been learning python from called Python Programming for the absolute beginner. I am at chapter 8, and I went through the final project below. The program is a virtual pet caretaker. You increase happiness by feeding and playing with the pet, but each time you do something the unhappiness is increased by 1.

The problem is that unhappiness seems to increase by 1, but it does not decrease when calling crit.play() or crit.eat(). I couldn't figure out why this is, and the source code that I downloaded from the book actually has the same problem.

Any help is very much appreciated!

# 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

    @property
    def mood(self):
        unhappiness = self.hunger + self.boredom
        if unhappiness < 5:
            m = "happy"
        elif 5 <= unhappiness <= 10:
            m = "okay"
        elif 11 <= unhappiness <= 15:
            m = "frustrated"
        else:
            m = "mad"
        return m
    
    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 = 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 = 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.") 




Is This A Good Question/Topic? 0
  • +

Replies To: Critter farm!

#2 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 117
  • View blog
  • Posts: 215
  • Joined: 11-January 13

Re: Critter farm!

Posted 24 June 2013 - 09:13 PM

Seems to work to me. Add some print lines to see what is going on. Unhappiness actually increases by two every turn not 1 though, so it is very hard to keep him happy.

Add this to your mood method:
print("Hunger: {},Boredom: {}".format(self.hunger,self.boredom))
print("Unhappiness: {}\n".format(unhappiness))
and add this at the bottom of the while loop of your main function:
print(crit.mood)
to get a better idea what is happening.

Sample:
Spoiler

-Mek
Was This Post Helpful? 1
  • +
  • -

#3 pandeee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 24-June 13

Re: Critter farm!

Posted 25 June 2013 - 09:34 PM

Thank you mekire
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1