[Tutor] Virtual pet cat program

myles broomes mylesbroomes at hotmail.co.uk
Wed Jan 18 00:37:55 CET 2012


I am trying to code a virtual pet cat program. Heres what I have come up with so far:
 
#Pet cat program
#the user chooses a cat of their choice; choosing attributes such as colour, name etc
#the user then has to look after the cat and keep it alive
#create a class for the cat 
class Cat(object):
        """A virtual pet cat"""
        def __init__(self, name, hunger = 5, boredom = 0, tiredness = 0):
                print("Meww!")
                self.name = name
                self.hunger = hunger
                self.boredom = boredom
                self.tiredness = tiredness
        def __str__(self):
                att = "Feline friend\n"
                att += "Name: " + self.name + "\n"
                if self.hunger >= 2:
                        print("Your pet is hungry.")
                elif self.boredom >= 2:
                        print("Your pet is bored.")
                elif self.tiredness >= 2:
                        print("Your pet is tired.")
                return att
        def __pass_time(self):
                self.hunger += 1
                self.boredom += 1
                self.tiredness += 1
        def eat(self, food = 5):
                print("Purrrr purrr!")
                self.hunger -= food
                if self.hunger < 0:
                        self.hunger = 0
                self.__pass_time()
        def play(self, fun = 5):
                print("Chirrup!")
                self.boredom -= fun
                if self.boredom < 0:
                        self.boredom = 0
                self.__pass_time()
        def rest(self, sleep = 5):
                print("...")
                self.tiredness -= sleep
                if self.tiredness < 0:
                        self.tiredness = 0
                self.__pass_time()
#introduce and explain the program to the user
input("Welcome to the pet cat simulation program. You will get the opportunity to look after a cat of choice. Press enter to begin.")
#get the users chosen attributes for their cat
name = input("Please enter a name for your cat: ")
user_cat = Cat(name)
#taking care of the cat loop
choice = None
while choice != "0":
        print("What would you like to do? (Enter 1 to feed the cat, enter 2 to play with the cat, enter 3 to leave the cat to rest or press 0 to exit the program.")
        choice = input(">")
        if choice == "1":
                user_cat.eat
                print(user_cat)
        elif choice == "2":
                user_cat.play
                print(user_cat)
        elif choice == "3":
                user_cat.rest
                print(user_cat)
input("Press enter to exit...") 
 
...Then I try to run the program:
 
Welcome to the pet cat simulation program. You will get the opportunity to look after a cat of choice. Press enter to begin.
Please enter a name for your cat: X
Meww!
What would you like to do? (Enter 1 to feed the cat, enter 2 to play with the cat, enter 3 to leave the cat to rest or press 0 to exit the program.
>1
Your pet is hungry.
Feline friend
Name: X
What would you like to do? (Enter 1 to feed the cat, enter 2 to play with the cat, enter 3 to leave the cat to rest or press 0 to exit the program.
>1
Your pet is hungry.
Feline friend
Name: X
What would you like to do? (Enter 1 to feed the cat, enter 2 to play with the cat, enter 3 to leave the cat to rest or press 0 to exit the program.
>1
Your pet is hungry.
Feline friend
Name: X
What would you like to do? (Enter 1 to feed the cat, enter 2 to play with the cat, enter 3 to leave the cat to rest or press 0 to exit the program.
>1
Your pet is hungry.
Feline friend
Name: X
What would you like to do? (Enter 1 to feed the cat, enter 2 to play with the cat, enter 3 to leave the cat to rest or press 0 to exit the program.
>0
Press enter to exit...
 
As you can see, no matter how many times I 'feed' the cat, its always hungry. Can someone please tell me why it wont update? 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120117/e5ec4024/attachment.html>


More information about the Tutor mailing list