0

This is probably a common error that many people get, but for me, I am trying to create a mythical creature generator where you can add and change attributes. Here is my code so far:

class Creature:
    def __init__(self):
        self.name = None
        self.feet = None
        self.inches = None
        self.weight = None
        self.gender = None

    def getName(self):
        return "Name: {}".format(self.name)

    def setName(self, name):
         self.name = name

    def getHeight(self):
        return "Height: {} ft. {} in.".format(self.feet, self.inches)

    def setHeight(self, feet, inches):
        self.feet = feet
        self.inches = inches

    def getWeight(self):
        return "Weight: {} lbs.".format(self.weight)

    def setWeight(self, weight):
        self.weight = weight

    def getGender(self):
        return "Gender: {}".format(self.gender)

    def setGender(self, index):
        genders = ['Male', 'Female', 'Others']
        if index == 0:
            self.gender = genders[0]
        elif index == 1:
            self.gender = genders[1]
        elif index == 2:
            self.gender = genders[2]


a = Creature()

a.setGender(input('Enter a value to set gender; 0 = male, 1 = female, 2 = others: '))
print(a.getGender())

However, my problem is how the setter/getter method for gender works. Even if I type in 0, 1, or 2 (the proper input values), I still get None returned to me. Had I not set up a None value in the constructor, I would've gotten an attribute error. I am trying to make it so that when I input in 0, I get male, 1 if female, and 2 if other. I did not include a condition where you must input a value again if input is too high or not an int.

  • "However, my problem is how the setter/getter method for gender works." First: what do you think "setter" and "getter" mean, and who told you to write the code this way? It's not the ordinary way of doing things in Python, and misses the point of using classes and objects properly. – Karl Knechtel Mar 29 '21 at 00:57
  • Anyway, none of the problem has *anything to do with classes at all*, and this is a sign that you are trying to run before you can walk. The problem is simply that *nothing the user types in* will *ever* be equal to *any* integer, because it is a string instead. – Karl Knechtel Mar 29 '21 at 01:02
  • "This is probably a common error that many people get" This is why it's important to try and diagnose and understand the problem first, so that you then have something meaningful that you can search for. In this case, you already were able to produce a specific example of the problem: you type in `0`, and then the result doesn't compare equal to `0`. The first thing to do is make as short of a program as you can think of to isolate that problem, and then attempt to verify what is going on - for example, by *checking the value and type of the variable* where you assigned the `input` result. – Karl Knechtel Mar 29 '21 at 01:05
  • When you would have done so, then you would see the problem, that you try to compare a string to an integer. That gives you something concrete to search about. If for example you decided that the solution would be to read the input as an integer directly, then that would lead you to find the linked duplicate question with a search engine. – Karl Knechtel Mar 29 '21 at 01:06

0 Answers0