-1

I'm a newcomer in python pure programming and I have created simply class "Human" and would like to make some objects ones in the dictionary with all attributes of initial class function "--inti--", and problem is, I can not specify that type of expected format for "gender" parameter, and if-else condition inside of --init-- does not work correctly, I mean it always fills in the gender parameter as "Female" while I added, for example, one Man and one Woman there are two types of gender but it just adds gender = 'Female' for each object

class Human:
    def __init__(self, name, gender, age):
            self.name = name

            if  ('Famele' or 'female' or 'f' or 'F' in gender):
                    gender = "Female"
                    self.gender= gender

            elif ('Male' or 'male' or 'm' or 'M' in gender):
                    gender = "Male"
                    self.gender = gender

            self.age = age

lis_obj_name = []
def obj_creator():
    obj_number = int(input("how many object would you like to create? "))
    for i in range(obj_number):
            my_object = input('what is object name? ')
            lis_obj_name.append(my_object)




obj_creator()


my_objects = {objects: Human(input('your name '),input('male,or female(Plz write it) '),input('your 
age ')) for objects in lis_obj_name}

or for example with this if-else formate, it still doesn't work! :

class Human:
    def __init__(self, name, gender, age):
            self.name = name

            if  gender == 'Famele' or 'female' or 'f' or 'F':
                    gender = "Female"
                    self.gender= gender

            elif gender == 'Male' or 'male' or 'm' or 'M':
                    gender = "Male"
                    self.gender = gender

            self.age = age
cs95
  • 379,657
  • 97
  • 704
  • 746
Ma H Di
  • 3
  • 2
  • 4
    Does this answer your question? [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – AMC Jun 28 '20 at 22:01
  • 1
    I agree with @AMC your Boolean logic looks fine from a human perspective, but that is not how python actually reads that specific Boolean expression. – Eno Gerguri Jun 28 '20 at 22:03
  • This question has the word "sexual" repeated too many times for comfort. – cs95 Jun 28 '20 at 22:05

1 Answers1

-1

The problem with your code is the statement if "Famele" or "female" or "f" or "F" in sexual. This statement will always evaluate to True so elif block will never execute.

You can change the implementation of your Human class to this, which uses in operator to check the gender.

class Human:
    def __init__(self, name, sexual, age):
        self.name = name

        if sexual in ["Famele", "female", "f", "F"]:
            sexual = "Female"
            self.sexual = sexual

        elif sexual in ["Male", "male", "m", "M"]:

            sexual = "Male"
            self.sexual = sexual

        self.age = age
Vishal Singh
  • 6,014
  • 2
  • 17
  • 33
  • https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value – AMC Jun 28 '20 at 22:05