asking user input through function in a class

This is the place for queries that don't fit in any of the other categories.

asking user input through function in a class

Postby vinchenzo » Wed Oct 23, 2013 9:24 am

Code: Select all
# Critter Caretaker
# A virtual pet to care for

class Critter(object):
    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, play = 3):
        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.")


Hi,

I would like to edit the def eat(self, food = 4) function so that I can accept user input.
I do that by:
Code: Select all
    def eat(self, food = 4):
        food = input("Enter number 1-4 for food")
        self.hunger -= food
        if self.hunger < 0:
            self.hunger = 0
        self.__pass_time()


Gets me the following error :
Code: Select all
    self.hunger -= self.inputfood
TypeError: unsupported operand type(s) for -=: 'int' and 'method


What am I doing wrong?
Last edited by micseydel on Wed Oct 23, 2013 6:25 pm, edited 1 time in total.
Reason: Code tags, locked.
vinchenzo
 
Posts: 1
Joined: Wed Oct 23, 2013 9:22 am

Re: asking user input through function in a class

Postby stranac » Wed Oct 23, 2013 9:34 am

Hi, please read this, and then fix your post.
It's unreadable like this, and I can't fix it because I can't tell from your post what you meant to post.
Also, please post the complete error traceback, not just a part of it.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1154
Joined: Thu Feb 07, 2013 3:42 pm

Re: asking user input through function in a class

Postby Kebap » Wed Oct 23, 2013 10:17 am

Hi vinchenzo, welcome to the forums! It seems to me, the error you posted is not for the code you posted. Maybe another version?
Last edited by micseydel on Wed Oct 23, 2013 6:25 pm, edited 1 time in total.
Reason: Removed unneeded quotation after editing OP.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 396
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe


Return to General Coding Help

Who is online

Users browsing this forum: conor100, Crimson King, Google [Bot], nuclearpengu1n and 3 guests